[英]How to find device location in a timely manner
I am creating an app that needs to find the location of the local device and compare it to the location of another device. 我正在创建一个需要查找本地设备位置并将其与另一台设备的位置进行比较的应用程序。 I am attempting to use the Google Play Services location apis to find the locations of the devices but it seems like it just takes way too long to connect.
我正在尝试使用Google Play服务的位置api查找设备的位置,但是似乎连接时间太长。 I haven't seen it connect yet.
我还没有看到连接。 It is either really slow or something isn't setup correctly.
速度确实很慢,或者设置不正确。 Are these apis normally pretty quick when connecting?
这些API在连接时通常很快吗?
Here is what I have: 这是我所拥有的:
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)
{
}
}
I have the following permissions: 我具有以下权限:
<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" />
I have also added this to the build.gradle: 我还将此添加到build.gradle中:
compile 'com.google.android.gms:play-services:9.2.0'
In this case I would use the LocationManager: 在这种情况下,我将使用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) {}
}
}
This approach is straight forward and allows you to skip dealing with Google Play Services. 这种方法简单明了,可让您跳过与Google Play服务的交易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.