gpt4 book ai didi

android - 如何在 Google map Android 上找到 MyLocationButton?

转载 作者:行者123 更新时间:2023-11-29 23:16:37 24 4
gpt4 key购买 nike

这就是我如何启用我的位置按钮,以便可以在 map 上单击它:

map.setMyLocationEnabled(true);

我的问题是,如何找到这个按钮并将其存储在一个变量中,以便根据我的应用程序的逻辑显示或不显示?谢谢!

最佳答案

你可以通过findViewWithTag("GoogleMapMyLocationButton")得到它在 MapFragment Root View 上。使用这样的代码:

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
...
private GoogleMap mGoogleMap;
private MapFragment mMapFragment;
private View mMyLocationButtonView = null;
...


@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int locationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
if (locationPermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String [] {Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE);
} else {
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
mMyLocationButtonView = mMapFragment.getView().findViewWithTag("GoogleMapMyLocationButton");
mMyLocationButtonView.setBackgroundColor(Color.GREEN);
}
}
}
}

你会得到类似的东西:

MyLocationButton with custom background

关于android - 如何在 Google map Android 上找到 MyLocationButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55222960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com