map.setmylocationenabled(true)不起作用 [英] map.setmylocationenabled(true) not working

查看:1061
本文介绍了map.setmylocationenabled(true)不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android应用中使用Google地图。我需要将地图重新​​映射到客户端的当前位置。我使用了以下语句:

  map.setmylocationenabled(true); 

显示右上角的按钮,但点击不起作用。



按钮点击侦听器:

  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;
}
});


解决方案

只需从我的其他答案在这里,并修改您的按钮点击侦听器以请求另一个位置:



<$ p $ (
$ b $ on $ MyBocationButtonClick(){
if(mGoogleApiClient!= null){$ b $(
$ b $ Override
public boolean onMyLocationButtonClick(){
$ mb.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener b LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this);
}
return false;
}
});

onLocationChanged()中的代码将重新居中摄像头位置,然后取消注册位置更新:

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

//放置当前位置标记
LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(当前位置);
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);

//移动地图相机
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(11));

if(mGoogleApiClient!= null){
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this);
}
}


I'm working with Google Maps in my android app. I need to recenter the map to the client's current location. I used the following statement -

map.setmylocationenabled(true);

This displays a button on the top right but clicking that doesn't work.

The button click listener:

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;
                }
            });

解决方案

Just take the code from my other answer here, and modify your button click listener to request another location:

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

The code in onLocationChanged() will then re-center the camera position, and then un-register for location updates again:

@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);
    }
}

这篇关于map.setmylocationenabled(true)不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆