グーグルマップは、ユーザーのlastKnownLocation
場所のためを使用していますlastknownLocation
が不明な場合、それは別のプロバイダを介して位置を取得するために時間がかかるGPS
かNetwork
を言います。 lastKnownLocationをベースロケーションとして使用し、LocationListenerでアップデートすることをお勧めします。
@Override
public void onMapReady(GoogleMap googleMap) {
if (ActivityCompat.checkSelfPermission
(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&&
ActivityCompat.checkSelfPermission
(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
requestPermissions(new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
}, 1);
}
else {
// enable location buttons
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
// fetch last location if any from provider - GPS.
final LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
final Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//if last known location is not available
if (loc == null) {
final LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
clearMap(googleMap); // clear map for every new location to update your marker unless `n` number of markers will form.
// getting location of user
final double latitude = location.getLatitude();
final double longitude = location.getLongitude();
final LatLng userCurrentLocation = new LatLng(latitude, longitude);
//focus added here **edited**
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userCurrentLocation, 14));
googleMap.addMarker(new MarkerOptions()
.position(userCurrentLocation)
.draggable(true)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.your_marker_icon_from_deawable)));
//do something with Lat and Lng, Parse to String if u want or set marker.
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
//when user enables the GPS setting, this method is triggered.
}
@Override
public void onProviderDisabled(String provider) {
//when no provider is available in this case GPS provider, trigger your gpsDialog here.
}
};
//update location every 10sec in 500m radius with both provider GPS and Network.
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10*1000, 500, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 500, locationListener);
}
else {
//do something with last known location.
// getting location of user
final double latitude = loc.getLatitude();
final double longitude = loc.getLongitude();
final LatLng userCurrentLocation = new LatLng(latitude, longitude);
//focus added here **edited**
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userCurrentLocation, 14));
googleMap.addMarker(new MarkerOptions()
.position(userCurrentLocation)
.draggable(true)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.your_marker_icon_from_deawable)));
}
}
}
許可取扱い:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults[0] != PackageManager.PERMISSION_GRANTED){
//do something
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
この例は、単なるデモンストレーション(例の目的)のためであり、私は方法を使って、自分のコードを最適化されていませんでした。今後編集してください。
あなたはすでに誰も同じ質問をしていないと思いますか? – Selvin
[AndroidマシュマロでGoogleマップ上の現在地を表示するにはどうすればいいですか?](http://stackoverflow.com/questions/34582370/how-can-i-show-current-location-on-a-google -map-on-android-marshmallow) –
@DanielNugentあなたの答えに続いて、私のアプリケーションがクラッシュしています。ライブラリを追加する必要はありますか? –