android谷歌地图 不点击myLocation按钮 直接定位到当前位置
问题描述:
安卓谷歌地图 进去时是显示世界地图 要点击mylocatonbutton才会定位到当前位置
需要怎么设置才能进去直接定位到当前位置 不需要点击myLocation按钮
答
把点击后定位的方法放在初始化里面实现
答
自己解决了
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, (LocationListener) MainActivity.this);
答
// 利用手机LocationManager,获取手机经纬度,然后谷歌定位到该经纬度
LocationManager mLocationManager = (LocationManager) getSystemService(getApplicationContext().LOCATION_SERVICE);
if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
/**参1:选择定位的方式
* 参2:定位的间隔时间
* 参3:当位置改变多少时进行重新定位
* 参4:位置的回调监听*/
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5*1000, 10, mLocationListener);
}
LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
LatLng currentPosition = new LatLng(location.getLatitude(), location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(currentPosition));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};