Google Maps v2 Android上的我的位置按钮未显示

时间:2013-04-22 14:59:53

标签: java android google-maps

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

这就是我的方式:

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);。我不知道发生了什么......

6 个答案:

答案 0 :(得分:54)

默认情况下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 Documentation
setMyLocationButtonEnabled Documentation

答案 1 :(得分:2)

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

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

就像这样

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

map.setMyLocationEnabled(true);

答案 2 :(得分:2)

添加以下行。

科特琳:

map.isMyLocationEnabled = true

map是GoogleMap对象。

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

答案 3 :(得分:0)

对于我来说, map.setMyLocationEnabled(true);
对于低于棉花糖的设备和高于棉花糖的设备,它工作正常,我在应用内手动设置了位置权限。 后来我的位置按钮可见了。

答案 4 :(得分: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)
    }
}

答案 5 :(得分: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);