map.setmylocationenabled(true)无法正常工作

Ven*_*ini 9 android location google-maps google-maps-markers

我在我的Android应用中使用谷歌地图.我需要将地图重新​​定位到客户端的当前位置.我使用了以下声明 -

map.setmylocationenabled(true);
Run Code Online (Sandbox Code Playgroud)

这会在右上方显示一个按钮,但单击该按钮不起作用.

按钮单击侦听器:

mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
                @Override
                public boolean onMyLocationButtonClick() {
                    mMap.addMarker(new MarkerOptions().position(myLatLng).title("My Location"));
                    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myLatLng, zoomLevel));
                    return false;
                }
            });
Run Code Online (Sandbox Code Playgroud)

Анд*_*чук 9

最后一行是我的解决方案:

myMap.setMyLocationEnabled(true);
myMap.getUiSettings().setMyLocationButtonEnabled(true);
Run Code Online (Sandbox Code Playgroud)


Dan*_*ent 5

只需从我的其他答案中获取代码,然后修改您的按钮点击监听器即可请求其他位置:

         mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
                @Override
                public boolean onMyLocationButtonClick() {
                     if (mGoogleApiClient != null) {
                         LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
                     }
                     return false;
                }
            });
Run Code Online (Sandbox Code Playgroud)

然后,onLocationChanged()中的代码将重新定位相机的位置,然后再次取消注册以更新位置:

@Override
public void onLocationChanged(Location location)
{
    mLastLocation = location;
    if (mCurrLocationMarker != null) {
        mCurrLocationMarker.remove();
    }

    //Place current location marker
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title("Current Position");
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
    mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);

    //move map camera
    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(11));

    if (mGoogleApiClient != null) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    }
}
Run Code Online (Sandbox Code Playgroud)