33

我正在使用谷歌地图做一个应用程序,但是当我尝试添加“我的位置”按钮时,正如参考所说的那样不起作用......

我就是这样做的:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

        locManager = (LocationManager)getSystemService(LOCATION_SERVICE);
        providersList = locManager.getAllProviders();
        provider =locManager.getProvider(providersList.get(0));
        precision = provider.getAccuracy();
        req = new Criteria();
        req.setAccuracy(Criteria.ACCURACY_FINE);        
        inside = false;

        map.getUiSettings().setMyLocationButtonEnabled(true);

        buildPolygon();
        drawPolygon();
        startLocalization();
    }

map.getUiSettings().setMyLocationButtonEnabled(true);在google的参考资料中使用了。我不知道发生了什么事。。

4

6 回答 6

57

myLocationButtonEnabled默认情况下为 true,并在启用图层时显示setMyLocationEnabled

尝试这个:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

    // Add this line
    map.setMyLocationEnabled(true);

    locManager = (LocationManager)getSystemService(LOCATION_SERVICE);
    providersList = locManager.getAllProviders();
    provider =locManager.getProvider(providersList.get(0));
    precision = provider.getAccuracy();
    req = new Criteria();
    req.setAccuracy(Criteria.ACCURACY_FINE);        
    inside = false;

    //map.getUiSettings().setMyLocationButtonEnabled(true);

    buildPolygon();
    drawPolygon();
    startLocalization();
}

setMyLocationEnabled 文档
setMyLocationButtonEnabled 文档

于 2013-04-22T15:05:56.387 回答
3

只需map.setMyLocationEnabled(true);在创建地图后添加

map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

变成这样

map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

map.setMyLocationEnabled(true);
于 2015-05-09T16:32:44.323 回答
2

添加以下行。

科特林:

map.isMyLocationEnabled = true

map是一个谷歌地图对象。

(此问题中更详细的答案:https ://stackoverflow.com/a/15142091/11211963 )

于 2020-04-07T15:41:16.017 回答
0

对我来说,map.setMyLocationEnabled(true);
它适用于棉花糖设备以下和棉花糖设备以上,我在应用程序内手动设置了位置权限。后来我的位置按钮变得可见。

于 2019-03-29T11:08:06.607 回答
0

上面的答案没有奏效。

override fun onMapReady(googleMap: GoogleMap?) {
    this.googleMap = googleMap

    setupMap()
}

private fun setupMap() {
    if (ActivityCompat.checkSelfPermission(context!!,
            Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        // Before enabling the My Location layer, you must request location permission from the user.
        googleMap?.isMyLocationEnabled = true
        // *** Use this method ***
        googleMap?.uiSettings?.isMyLocationButtonEnabled = true

        // See https://developers.google.com/maps/documentation/android-sdk/location
        googleMap?.setOnMyLocationButtonClickListener(this)
        googleMap?.setOnMyLocationClickListener(this)
    } else {
        // Show rationale and request permission.
        requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
            LOCATION_REQUEST_CODE)
    }
}
于 2019-05-28T15:54:29.987 回答
0

您需要获得许可才能使其可见

if (ActivityCompat.checkSelfPermission(Maps_Activity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(Maps_Activity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

   requestPermission();

   return;
}

mMap.setMyLocationEnabled(true);

    
于 2021-02-16T10:22:38.787 回答