Bas*_*sit 1 android google-maps-android-api-2
我想在Google Maps Android API v2中启用位置.我想要的是:
我对如何定期更新,选择时间间隔的数量感到困惑.现在,我已经设置了项目来显示地图并使用
googleMap.setMyLocationEnabled(true);
googleMap.setOnMyLocationChangeListener(myLocationChangeListener);
private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
if(googleMap != null){
if(!firstTime){
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(loc).zoom(zoom).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
firstTime = true;
}
lat = location.getLatitude();
lng = location.getLongitude();
Toast.makeText(context, "Location is: " + String.valueOf(lat) + ", "
+ String.valueOf(lng), Toast.LENGTH_LONG).show();
}
}
};
Run Code Online (Sandbox Code Playgroud)
在变量中设置lat/lng.我的问题是:
问题2:
使用 requestLocationUpdates(long minTime, float minDistance, Criteria criteria, PendingIntent intent)
前两个参数是:
参数minTime:位置更新之间的最小时间间隔,以毫秒为单位minDistance:位置更新之间的最小距离(以米为单位)
private static final long POLLING_FREQ = 1000 * 10;
private static final float MIN_DISTANCE = 10.0f;
// Reference to the LocationManager and LocationListener
private LocationManager mLocationManager;
private LocationListener mLocationListener;
// Register for network location updates
if (null != mLocationManager
.getProvider(LocationManager.NETWORK_PROVIDER)) {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, POLLING_FREQ,
MIN_DISTANCE, mLocationListener);
}
// Register for GPS location updates
if (null != mLocationManager
.getProvider(LocationManager.GPS_PROVIDER)) {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, POLLING_FREQ,
MIN_DISTANCE, mLocationListener);
}
Run Code Online (Sandbox Code Playgroud)
您可以根据自己的需要进行调整.
问题3:
使用:removeUpdates(PendingIntent intent)删除指定挂起意图的所有位置更新.
截至问题1.
你的方法似乎合理.如果你需要不断的位置更新,那么我可以建议其他的东西来减少电池消耗.
mLocationListener = new LocationListener() {
// Called back when location changes
public void onLocationChanged(Location location) {
// Get your location here
// estimate
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
// NA
}
public void onProviderEnabled(String provider) {
// NA
}
public void onProviderDisabled(String provider) {
// NA
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5389 次 |
| 最近记录: |