简体   繁体   English

GoogleMaps v2 Android 的我的位置按钮,未显示

[英]My-Location Button of GoogleMaps v2 Android, not displayed

I'm doing an app, with google maps, but when I try to add "my-location" button, as the reference says doesn't work...我正在做一个带有谷歌地图的应用程序,但是当我尝试添加“我的位置”按钮时,正如参考资料所说的那样不起作用......

thats how I do:我就是这样做的:

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

I used map.getUiSettings().setMyLocationButtonEnabled(true);我使用了map.getUiSettings().setMyLocationButtonEnabled(true); as shows in the reference of google.如谷歌的参考所示。 I don't know what's going on..我不知道怎么回事。。

The myLocationButtonEnabled is true by default and shown when the setMyLocationEnabled layer is enabled.默认情况下myLocationButtonEnabled为 true 并在启用setMyLocationEnabled层时显示。

try this:试试这个:

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 setMyLocationEnabled 文档
setMyLocationButtonEnabled Documentation setMyLocationButtonEnabled 文档

just add map.setMyLocationEnabled(true);只需添加map.setMyLocationEnabled(true); after create your map创建地图后

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

to be like this像这样

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

map.setMyLocationEnabled(true);

Add the following line.添加以下行。

Kotlin:科特林:

map.isMyLocationEnabled = true

map is a GoogleMap object. map是一个 GoogleMap 对象。

(More detailed answer(s) in this question: https://stackoverflow.com/a/15142091/11211963 ) (这个问题更详细的答案: https : //stackoverflow.com/a/15142091/11211963

For me, map.setMyLocationEnabled(true);对我来说, map.setMyLocationEnabled(true);
It worked fine for below marshmallow devices and above marshmallow devices, I gave Location permission manually in-app settings.它适用于低于棉花糖设备和高于棉花糖设备,我在应用程序设置中手动授予位置权限。 Later My Location Button got visible.后来我的位置按钮变得可见。

The above answers didn't work.上面的答案没有用。

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

You need permission to make it visible您需要获得许可才能使其可见

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

    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM