OGeek|极客世界-中国程序员成长平台

标题: ios - MKMapItem 未在 MKPlacemark 上显示正确信息 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 11:40
标题: ios - MKMapItem 未在 MKPlacemark 上显示正确信息

我得到了 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”。

我发现很难找到任何关于从 MKMapItemMKPlacemark 中获取内容的信息。

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

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



Best Answer-推荐答案


MKPlacemark 返回其 title 属性的地址,而 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 属性的自定义类(或返回 MKMapItem 的某些类code>name 作为它的 title)。

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

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

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






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4