应用程序启动地图活动后,如何在 android 的地图中显示我的当前位置
Posted
技术标签:
【中文标题】应用程序启动地图活动后,如何在 android 的地图中显示我的当前位置【英文标题】:How to show my current location in maps in android as soon as the app starts the map activity 【发布时间】:2017-04-21 15:41:21 【问题描述】:该怎么做,一旦我打开我的应用程序的地图活动,它就会在谷歌地图上显示我当前的位置,并用当前街道地址填充文本视图。我搜索了很多但没有任何方法都可以做到这一点。即使运行时权限也不起作用。将标记获取到我当前位置的按钮没有响应。没有显示错误。 这是我的地图活动。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
GoogleMap.OnMarkerDragListener,
GoogleMap.OnMapLongClickListener, View.OnClickListener
private GoogleMap mMap;
private double vlatitude, vlongitude;
private GoogleApiClient vgoogleApiClient;
private Button getButton;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
//Initializing googleapi client
vgoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
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;
mMap.setMyLocationEnabled(true);
getButton = (Button) findViewById(R.id.buttongetme);
getButton.setOnClickListener(this);
@Override
protected void onStart()
vgoogleApiClient.connect();
super.onStart();
@Override
protected void onStop()
vgoogleApiClient.disconnect();
super.onStop();
@Override
public void onMapReady(GoogleMap googleMap)
mMap = googleMap;
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.mipmap.tiltpink)).position(sydney).draggable(true).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mMap.setOnMarkerDragListener(this);
mMap.setOnMapLongClickListener(this);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
private void getCurrentLocation()
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;
Location location = LocationServices.FusedLocationApi.getLastLocation(vgoogleApiClient);
if (location != null)
vlongitude = location.getLongitude();
vlatitude = location.getLatitude();
moveMap();
private void moveMap()
LatLng latLng = new LatLng(vlatitude,vlongitude);
mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.mipmap.tiltpink)).position(latLng).draggable(true).title("Current Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
@Override
public void onConnected(@Nullable Bundle bundle)
getCurrentLocation();
@Override
public void onConnectionSuspended(int i)
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
@Override
public void onMapLongClick(LatLng latLng)
mMap.clear();
mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.mipmap.tiltpink)).position(latLng).draggable(true).title("This is my loc"));
@Override
public void onMarkerDragStart(Marker marker)
@Override
public void onMarkerDrag(Marker marker)
@Override
public void onMarkerDragEnd(Marker marker)
vlatitude = marker.getPosition().latitude;
vlongitude = marker.getPosition().longitude;
moveMap();
@Override
public void onClick(View view)
if (view == getButton)
getCurrentLocation();
moveMap();
【问题讨论】:
如果您不能回答问题,请不要投反对票。本网站旨在帮助高级和新开发人员,而不是为那些对无法回答的问题投反对票感觉良好的白痴提供帮助。 【参考方案1】:这个方法解决了问题。
@Override
public void onLocationChanged(Location location)
if (location != null)
LatLng latLang = new LatLng(location.getLatitude(),
location.getLongitude());
animateCameraToMarker(latLang, false);
setMarker(latLang);
getAddressFromLocation(latLang, source);
// stop location updates
if (mGoogleApiClient != null)
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
【讨论】:
以上是关于应用程序启动地图活动后,如何在 android 的地图中显示我的当前位置的主要内容,如果未能解决你的问题,请参考以下文章