如何及时找到设备位置
How to find device location in a timely manner
我正在创建一个应用程序,它需要找到本地设备的位置并将其与另一台设备的位置进行比较。我正在尝试使用 Google Play Services location api 来查找设备的位置,但似乎连接时间太长了。我还没有看到它连接。它要么真的很慢,要么设置不正确。这些 api 在连接时通常很快吗?
这是我的资料:
public class LocationHandler implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
{
private Activity currActivity = null;
private GoogleApiClient mGoogleApiClient = null;
public LocationHandler(Activity act)
{
currActivity = act;
}
public void Setup()
{
if (mGoogleApiClient == null)
{
mGoogleApiClient = new GoogleApiClient.Builder(currActivity)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
}
@Override
public void onConnected(@Nullable Bundle bundle)
{
Toast.makeText(currActivity, "Connected to service.", Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionSuspended(int i)
{
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
{
}
}
我有以下权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.INTERNET" />
我也已将此添加到 build.gradle:
compile 'com.google.android.gms:play-services:9.2.0'
在这种情况下,我会使用 LocationManager:
public class LocationHandler implements implements LocationListener{
LocationManager mLocationManager;
public void Setup(){
mLocationManager = (LocationManager) getApplicationContext()
.getSystemService(LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
500, 10, this);
}
@Override
public void onLocationChanged(Location location) {
//do something with your location
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
}
}
这种方法很简单,可以让您跳过处理 Google Play 服务。
我正在创建一个应用程序,它需要找到本地设备的位置并将其与另一台设备的位置进行比较。我正在尝试使用 Google Play Services location api 来查找设备的位置,但似乎连接时间太长了。我还没有看到它连接。它要么真的很慢,要么设置不正确。这些 api 在连接时通常很快吗?
这是我的资料:
public class LocationHandler implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
{
private Activity currActivity = null;
private GoogleApiClient mGoogleApiClient = null;
public LocationHandler(Activity act)
{
currActivity = act;
}
public void Setup()
{
if (mGoogleApiClient == null)
{
mGoogleApiClient = new GoogleApiClient.Builder(currActivity)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
}
@Override
public void onConnected(@Nullable Bundle bundle)
{
Toast.makeText(currActivity, "Connected to service.", Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionSuspended(int i)
{
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
{
}
}
我有以下权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.INTERNET" />
我也已将此添加到 build.gradle:
compile 'com.google.android.gms:play-services:9.2.0'
在这种情况下,我会使用 LocationManager:
public class LocationHandler implements implements LocationListener{
LocationManager mLocationManager;
public void Setup(){
mLocationManager = (LocationManager) getApplicationContext()
.getSystemService(LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
500, 10, this);
}
@Override
public void onLocationChanged(Location location) {
//do something with your location
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
}
}
这种方法很简单,可以让您跳过处理 Google Play 服务。