我想知道如何在地图加载时显示当前用户位置一次,而无需再次检查位置更改和相机移动,除非用户询问并点击它。我看到很多关于按钮点击事件和获取当前loc的教程,但没有一个帮助。我看到的另一个是旧的已弃用的类(LocationClient等...)。
任何帮助将不胜感激! 非常感谢!
答案 0 :(得分:6)
首先检查位置是否启用..如果您的位置已启用,则地图将自动加载。否则没有。
这是我的代码......
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
mMapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
try {
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
overridePendingTransition(R.anim.push_up_in,
R.anim.push_up_out);
} else {
mMapFragment.getMapAsync(this);
overridePendingTransition(R.anim.push_up_out,
R.anim.push_up_in);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (isLocationEnabled(MapsActivity.this)) {
manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
mCriteria = new Criteria();
bestProvider = String.valueOf(manager.getBestProvider(mCriteria, true));
mLocation = manager.getLastKnownLocation(bestProvider);
if (mLocation != null) {
Log.e("TAG", "GPS is on");
final double currentLatitude = mLocation.getLatitude();
final double currentLongitude = mLocation.getLongitude();
LatLng loc1 = new LatLng(currentLatitude, currentLongitude);
mMap.addMarker(new MarkerOptions().position(loc1).title("Your Current Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(currentLatitude, currentLongitude), 15));
mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
} else {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
return;
}
manager.requestLocationUpdates(bestProvider, 1000, 0, (LocationListener) this);
}
setupMap();
}
private void setupMap() {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
AppSettings settings = AppSettings.getSettings(getApplication());
String m = settings.getMapType();
try {
if (m.contentEquals(null)) {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
} else if (m.contentEquals("NORMAL")) {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
} else if (m.contentEquals("HYBRID")) {
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
} else if (m.contentEquals("SATELLITE")) {
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
} else if (m.contentEquals("TERRAIN")) {
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
} else {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
} catch (NullPointerException e) {
e.printStackTrace();
}
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
mMap.getUiSettings().setIndoorLevelPickerEnabled(true);
mMap.setBuildingsEnabled(true);
mMap.setIndoorEnabled(true);
}
此代码仅适用于Google地图v2。
当你要启用GPS设置时,你的地图处于onPause()状态,所以尝试处理android生命周期。
如果需要,请将此行置于OnResume()状态..
@Override
protected void onResume() {
super.onResume();
mMapFragment.getMapAsync(this);
}
答案 1 :(得分:0)
boolean isFirstTime = true;
if (mMap != null)
if (mMap != null)
if (isFirstTime) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate center =
CameraUpdateFactory.newLatLng(latLng);
CameraUpdate zoom = CameraUpdateFactory.zoomTo(15.0f);
mMap.moveCamera(center);
mMap.animateCamera(zoom);
isFirstTime = false;
}