Google 地图 android - 标题未出现在标记上

发布于 2025-01-17 21:07:52 字数 3935 浏览 0 评论 0原文

我正在使用谷歌地图构建一个应用程序,当用户单击标记时,它将显示有关兴趣点的信息/照片。主要活动有两个片段,两者都是可见的(屏幕按 50/50 分割)。第一个片段显示地图,第二个片段显示有关用户单击的任何标记的信息/照片。标记详细信息(纬度、经度、标题)和有关兴趣点的文本描述保存在自定义对象 PointOfInterest 中。

PointOfInterest 对象存储在 ArrayList 中,我的 onMapReady() 方法调用名为 addAllMarkers() 的方法来循环遍历 ArrayList 并更新带有所有标记的地图。

到目前为止,一切正常,所有标记都显示,但标题不显示。这是我添加标记的代码:

private void addAllMarkers() {

    for(PointOfInterest pointOfInterest : ((MainActivity) getActivity()).getPointsOfInterest().values()) {
        addPointOfInterestMarker(pointOfInterest);
    }
}

public void addPointOfInterestMarker(PointOfInterest pointOfInterest) {

    double latitude = pointOfInterest.getLatitude();
    double longitude = pointOfInterest.getLongitude();
    String placeTitle = pointOfInterest.getPlaceTitle();

    LatLng position = new LatLng(latitude, longitude);

    mMap.addMarker(new MarkerOptions()
            .position(position)
            .title(placeTitle));
}

即使我将 .title(placeTitle) 替换为 .title("Some Text"),我也不会出现任何标题。这是显示标记的屏幕打印,但没有标题。

屏幕打印显示标记,但没有标题

我想要的是如下例所示的文本标题:

显示标题的屏幕打印

地图片段的 XML 为:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map_fragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.MapsFragment" />

这里是 onMapReady() 方法:

   public void onMapReady(GoogleMap googleMap) {

        mMap = googleMap;

        // following code is to update location to current location, provided user has
        // given permission to do so.
        if(mMap != null) {
            Context context = getActivity().getApplicationContext();
            int permission = ContextCompat.checkSelfPermission(context,
                    Manifest.permission.ACCESS_FINE_LOCATION);

            if(permission == PackageManager.PERMISSION_GRANTED) {
                mMap.setMyLocationEnabled(true);
            } else {
                requestPermission(Manifest.permission.ACCESS_FINE_LOCATION,
                        LOCATION_REQUEST_CODE);
            }
        }

        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

        // add all the other markers
        addAllMarkers();

        // display user controls i.e., zoom in and out buttons, my location button etc
        UiSettings mapSettings;
        mapSettings = mMap.getUiSettings();
        mapSettings.setZoomControlsEnabled(true);
        mapSettings.setScrollGesturesEnabled(true);
        mapSettings.setTiltGesturesEnabled(true);
        mapSettings.setRotateGesturesEnabled(true);
        mapSettings.setMyLocationButtonEnabled(true);

        mMap.setPadding(0, 0, 0, 0);

        // zoom in on the centre of Clovelly
        LatLng clovelly = new LatLng(50.998128, -4.399118);
        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(clovelly)
                .zoom(16)
                .build();
        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        // listener for markers
        mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                markerClicked(marker);
                return true;
            }
        });
    }

我已在几个不同的模拟器和物理设备上运行代码,但无法显示标题。

I am building an app using google maps, that will display information/photos about points of interest when a user clicks on a marker. The main activity has two fragments, both are visible (the screen is split 50/50). The first fragment displays a map, the second fragment shows information/photos about whichever marker the user has clicked on. The marker details (latitude, longitude, title) and a text description about the point of interest are held in a custom object PointOfInterest.

The PointOfInterest objects are stored in an ArrayList, and my onMapReady() method calls a method called addAllMarkers() to loop through the ArrayList and update the map with all the markers.

So far, everything works fine, all the markers display, however the titles do not display. Here is the code where I add the markers:

private void addAllMarkers() {

    for(PointOfInterest pointOfInterest : ((MainActivity) getActivity()).getPointsOfInterest().values()) {
        addPointOfInterestMarker(pointOfInterest);
    }
}

public void addPointOfInterestMarker(PointOfInterest pointOfInterest) {

    double latitude = pointOfInterest.getLatitude();
    double longitude = pointOfInterest.getLongitude();
    String placeTitle = pointOfInterest.getPlaceTitle();

    LatLng position = new LatLng(latitude, longitude);

    mMap.addMarker(new MarkerOptions()
            .position(position)
            .title(placeTitle));
}

Even if I replace .title(placeTitle) with .title("Some Text"), I get no title appearing. Here is a screen print showing the markers, but no titles.

Screen print showing the markers, but no titles

What I want is text titles like the following example:

screen print showing titles

The XML for the map fragment is:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map_fragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.MapsFragment" />

And here is the onMapReady() method:

   public void onMapReady(GoogleMap googleMap) {

        mMap = googleMap;

        // following code is to update location to current location, provided user has
        // given permission to do so.
        if(mMap != null) {
            Context context = getActivity().getApplicationContext();
            int permission = ContextCompat.checkSelfPermission(context,
                    Manifest.permission.ACCESS_FINE_LOCATION);

            if(permission == PackageManager.PERMISSION_GRANTED) {
                mMap.setMyLocationEnabled(true);
            } else {
                requestPermission(Manifest.permission.ACCESS_FINE_LOCATION,
                        LOCATION_REQUEST_CODE);
            }
        }

        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

        // add all the other markers
        addAllMarkers();

        // display user controls i.e., zoom in and out buttons, my location button etc
        UiSettings mapSettings;
        mapSettings = mMap.getUiSettings();
        mapSettings.setZoomControlsEnabled(true);
        mapSettings.setScrollGesturesEnabled(true);
        mapSettings.setTiltGesturesEnabled(true);
        mapSettings.setRotateGesturesEnabled(true);
        mapSettings.setMyLocationButtonEnabled(true);

        mMap.setPadding(0, 0, 0, 0);

        // zoom in on the centre of Clovelly
        LatLng clovelly = new LatLng(50.998128, -4.399118);
        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(clovelly)
                .zoom(16)
                .build();
        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        // listener for markers
        mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                markerClicked(marker);
                return true;
            }
        });
    }

I have run the code on a couple of different emulators and a physical device, but cannot get the titles to display.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文