ios - MKMapItem 未在 MKPlacemark 上显示正确的信息

标签 ios objective-c mkmapview mkannotation mkmapitem

我得到了 MKLocalSearch 的结果,它包括类似...

{
    address =     {
        formattedAddressLine =         (
            "Marton Road",
            Middlesbrough,
            TS1,
            England
        );
        structuredAddress =         {
            administrativeArea = England;
            areaOfInterest =             (
                "Great Britain"
            );
            country = "United Kingdom";
            countryCode = GB;
            fullThoroughfare = "Marton Road";
            geoId =             (
            );
            locality = Middlesbrough;
            postCode = TS1;
            subAdministrativeArea = Middlesbrough;
            thoroughfare = "Marton Road";
        };
    };
    addressGeocodeAccuracy = 0;
    business =     (
                {
            UID = 9301704419119613323;
            URL = "http://www.cineworld.co.uk";
            attribution =             (
                                {
                    attributionURLs =                     (
                        "yelp5.3:///biz/cineworld-middlesbrough",
                        "yelp4:///biz/cineworld-middlesbrough",
                        "yelp:///biz/cineworld-middlesbrough",
                        "http://yelp.com/biz/cineworld-middlesbrough"
                    );
                    sourceIdentifier = "com.yelp";
                    sourceVersion = 1;
                }
            );
            canBeCorrectedByBusinessOwner = 1;
            name = Cineworld;
            source =             (
                                {
                    "source_id" = "b2LOPag6ha6845__dgXehw";
                    "source_name" = yelp;
                },
                                {
                    "source_id" = 6670;
                    "source_name" = tribune;
                },
                                {
                    "source_id" = 2000000103009680;
                    "source_name" = "acxiom_intl";
                },
                                {
                    "source_id" = "cineworld-middlesbrough";
                    "source_name" = "yelp_alias";
                }
            );
            "star_rating" =             (
                0
            );
            telephone = "+448712002000";
        }
    );
    center =     {
        lat = "54.57633773904653";
        lng = "-1.228197113614671";
    };
    inputLanguage = en;
    localSearchProviderID = 9902;
    mapRegion =     {
        eastLng = "-1.224891596539819";
        northLat = "54.57545000290778";
        southLat = "54.5738619816233";
        westLng = "-1.227631256834202";
    };
    name = Cineworld;
    type = 57;
}

现在,当我将它添加到我的 map 时...

id <MKAnnotation> annotation = mapItem.placemark;

[self.mapView addAnnotation:annotation];

它添加了一个图钉,当我点击它时,它会显示“Marton Road”,但我希望它显示“Cineworld”。

我发现很难找到任何有关将内容从 MKMapItem 中取出并放入 MKPlacemark 的信息。

如果我尝试在地标中使用mapItem.name,那么它们都会显示“美国”。

知道如何从中获得一些更有用的信息吗?

最佳答案

MKPlacemark返回其 title 的地址属性(property)和MKPlacemark类不允许您设置 title你自己。

您可以做的是创建一个 MKPointAnnotation (具有可设置的 title 属性)并设置其 title任何你想要的,比如mapItem.name .

例如:

MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.title = mapItem.name;
pa.coordinate = mapItem.placemark.coordinate;
[self.mapView addAnnotation:pa];


注意:
您不必使用MKPointAnnotation类(这只是最方便的)。
您还可以使用符合 MKAnnotation 的自定义类并且有一个可设置的 title属性(或某个返回 MKMapItemname 作为其 title 的类)。

另请注意,如果您希望能够访问相关的 MKMapItemMKPlacemark在事后添加的注释中(例如在 map View 委托(delegate)方法中),您需要使用自定义类而不是 MKPointAnnotation您可以在其中添加“sourceMapItem”或“sourcePlacemark”属性,您可以在创建注释时设置这些属性。

这样,您可以设置 title根据需要但仍可访问所有原始 MKMapItemMKPlacemark创建注释对象的值(如果仅使用 MKPointAnnotation,则无法轻松完成此操作,因为它不保留对源 map 项或地标的引用)。

关于ios - MKMapItem 未在 MKPlacemark 上显示正确的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25532687/

相关文章:

xcode - 当 MKMAPVIEW 范围改变时触发什么事件

ios - 在 objective-c 中解析JSON信息(多个级别)

Objective-C 中的 C++ 样式函数头?

ios - 如何更新 CoreData 中另一个实体的子实体

ios - Objective-C iOS 中的谷歌地图集成问题(_ OBJC _ CLASS _ $ _ GMSPlacesClient,引用自 :)

objective-c - MKMapView 上的 UIPanGestureRecognizer?

ios - 带有集群注释的 map View

ios - 如果对象无论如何都将被销毁,为什么我们在 dealloc 时将委托(delegate)设置为 nil?

ios - 应用程序因内存错误而终止 - iPad 应用程序

通过 Testflight 存档和安装应用程序时,iOS Firebase crashlytics 仪表板不会显示崩溃