简体   繁体   English

使用 Google 地图 API 时没有显示“我的当前位置”按钮

[英]No 'My Current Location' button showing when using Google Maps API

I am trying to write an app for searching nearest places of interest, for example restaurants.我正在尝试编写一个应用程序来搜索最近的景点,例如餐馆。 My problem is that despite seemingly proper coding, the button that should move the map view to the current location does not appear.我的问题是,尽管看起来编码正确,但应该将 map 视图移动到当前位置的按钮没有出现。 Here's my code, an excerpt from onMapReady function:这是我的代码,摘自 onMapReady function:

 if (mapView != null && mapView.findViewById(Integer.parseInt("1")) != null) {
            //View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("4"));
            View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            layoutParams.setMargins(0, 0, 30, 30);
        }      

I have tried looking for answers, but most of already given answers concern problems with just positioning and not not appearing at all.我曾尝试寻找答案,但大多数已经给出的答案都涉及仅定位而根本没有出现的问题。 Android: Google Maps API - Change position of maps toolbar This question concerns the same problem, but in my application permissions are given on runtime, at least I think they are (There's a separate activity for grating permissions when the aplication is started for the first time). Android:谷歌地图 API - 更改地图权限工具栏的 position这个问题涉及到同样的问题,但在我的应用程序中,权限是在运行时给出的,至少我认为它们是在第一个应用程序启动时单独活动时间)。 My location button is not showing in map fragment 我的位置按钮未显示在 map 片段中

Is this is what you are searching for?这就是您要搜索的内容吗?

map.setMyLocationEnabled(true);

map is object of GoogleMap map 是 GoogleMap 的 object

you need to request permissios ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION at runtime您需要在运行时请求权限 ACCESS_COARSE_LOCATION 和 ACCESS_FINE_LOCATION

@Override
public void onMapReady(GoogleMap googleMap) {
            mMap= googleMap;
            if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            mMap.setMyLocationEnabled(true);
            mMap.getUiSettings().setMyLocationButtonEnabled(true);

}

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

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