最初に位置が有効かどうかを確認します。あなたの位置が有効な場合、地図は自動的に読み込まれます。そうでなければここ
は、両方の事のために私のコード..です
@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()状態になっていますので、アンドロイドのライフサイクルを処理してみてください。そのが必要な場合はOnResume()状態で、この行を入れ
..
@Override
protected void onResume() {
super.onResume();
mMapFragment.getMapAsync(this);
}
は偉大な作業、あなたの先生ありがとうございました! – 2Stoned