如何访问 MKMapItem 的类别或类型

问题描述 投票:0回答:4

我正在编写一个使用 MapKit 的应用程序。我已经实现了 MKLocalSearch 并且得到了 MKMapItem 的数组。然而,我想知道是否有可能获得这些项目的类别。例如,在地图应用程序中,商店、酒店、火车站等会显示不同的图标。此外,如果您查看地点标记。您会获得一个类别标签,例如杂货店。作为开发人员,我可以访问地图项的信息吗?如果是的话我想知道如何。

谢谢你

ios swift mapkit core-location mkmapitem
4个回答
4
投票

是的,您可以获得此信息。请参阅以下方法以获取搜索位置的信息详细信息。

恐怕您只能从

MKPlacemark
获取地址详细信息。

现在您要做的是,从

MKPlacemark
获取地址详细信息,并且您需要借助任何开源 API 来帮助您将地址分类为某些标签/注释。

其中一个很好的 API 是 Mapbox,但不幸的是它是付费的。

因此,您可以通过第三方 API 进行神奇的搜索。我还没有搜索过 API / WebService 的种类,但它应该在那里。

目标C代码:

- (void) searchForPlace:(NSString *) keyWord {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
    request.naturalLanguageQuery = keyWord; // @"restaurant"
    MKCoordinateSpan span = MKCoordinateSpanMake(.1, .1);

    CLLocationCoordinate2D location = self.mapView.centerCoordinate;
    request.region = MKCoordinateRegionMake(location, span);
    MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];

    [search startWithCompletionHandler:
     ^(MKLocalSearchResponse *response, NSError *error) {
         [self.txtSearch setEnabled:YES];
         [self removeMapOverlay];

         [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
         if (!error) {
             // Result found
             @try {

                 if (response.mapItems && [response.mapItems count] > 0) {

                     for (MKMapItem *item in response.mapItems) {
                         MKPlacemark *placeMark = item.placemark;

                         // Address details
                         NSDictionary *address = placeMark.addressDictionary;
                         NSString *titleString = @"";
                         NSString *subtitleString = @"";
                         NSString *name = @"";
                         NSString *Thoroughfare = @"";
                         NSString *State = @"";
                         NSString *City = @"";
                         NSString *Country = @"";

                         name = [address objectForKey:@"Name"] ? [address objectForKey:@"Name"] : @"";
                         Thoroughfare = [address objectForKey:@"Thoroughfare"] ? [address objectForKey:@"Thoroughfare"] : @"";
                         State = [address objectForKey:@"State"] ? [address objectForKey:@"State"] : @"";
                         City = [address objectForKey:@"City"] ? [address objectForKey:@"City"] : @"";
                         Country = [address objectForKey:@"Country"] ? [address objectForKey:@"Country"] : @"";

                         titleString = [NSString stringWithFormat:@"%@ %@", name, Thoroughfare];
                         subtitleString = [NSString stringWithFormat:@"%@ %@ %@", State, City, Country];

                         CustomAnnotation *annotation = [[CustomAnnotation alloc] initWithTitle:titleString subTitle:subtitleString detailURL:item.url location:placeMark.location.coordinate];
                         [self.mapView addAnnotation:annotation];
                     }
                     [self mapView:self.mapView regionDidChangeAnimated:YES];
                 }

             }
             @catch (NSException *exception) {
                 NSLog(@"Exception :%@",exception.description);
             }

         } else {
             NSLog(@"No result found.");
         }
     }];
}

快速代码:

func searchForPlace(keyword: String) {

    UIApplication.sharedApplication().networkActivityIndicatorVisible = true

    var requset = MKLocalSearchRequest()
    requset.naturalLanguageQuery = keyword

    let span = MKCoordinateSpanMake(0.1, 0.1)
    let region = MKCoordinateRegion(center: self.mapView.centerCoordinate, span: span)

    var search = MKLocalSearch(request: requset)

    search.startWithCompletionHandler { (var response: MKLocalSearchResponse!, var error: NSError!) -> Void in
        UIApplication.sharedApplication().networkActivityIndicatorVisible = false

        if (error != nil) {
            // Result found

            if (response.mapItems != nil && response.mapItems.count > 0) {

                for item: MKMapItem! in response.mapItems as [MKMapItem] {
                    var placeMark = item.placemark as MKPlacemark!

                    // Address details...
                    var address = placeMark.addressDictionary as NSDictionary!
                    var titleString: String!
                    var subtitleString: String!
                    var name: String!
                    var Thoroughfare: String!
                    var State: String!
                    var City: String!
                    var Country: String!

                    var emptyString: String! = " "

                    name = (address.objectForKey("name") != nil ? address.objectForKey("name") : emptyString) as String
                    Thoroughfare = (address.objectForKey("Thoroughfare") != nil ? address.objectForKey("Thoroughfare") : emptyString) as String
                    State = (address.objectForKey("State") != nil ? address.objectForKey("State") : emptyString) as String
                    City = (address.objectForKey("City") != nil ? address.objectForKey("City") : emptyString) as String
                    Country = (address.objectForKey("Country") != nil ? address.objectForKey("Country") : emptyString) as String

                    titleString = String(format: "%@ %@", name, Thoroughfare)
                    subtitleString = String(format: "%@ %@ %@", State, City, Country)

                    var customAnnotation = CustomAnnotation(coordinate: placeMark.location.coordinate, title: titleString, subtitle: subtitleString, detailURL: item.url)
                    self.mapView.addAnnotation(customAnnotation)
                }

                self.mapView(self.mapView, regionDidChangeAnimated: true)
            }
        }

    }
}

1
投票

我知道这是一个老问题,但我偶然发现了同样的需求 - 从

MKMapItem
对象获取类别信息。

问: 可以从

MKMapItem
获取分类信息吗?

A:是的,这是可能的。您可以从地图项中提取它。


问: Apple 允许这样做吗?

A: 不是真的......您必须使用键值来获取这些属性,因此它技术上将访问私有框架。 但是,你没有问是否必须“允许”。


这是我的一个很好的方法(它是在 Objc 中,但为你将其转换为 swift,所以如果有任何错误请原谅我 - 还没有测试过)

// Let "item" be some MKMapItem

let geo_place = item.value(forKey: "place") as! NSObject
let geo_business = geo_place.value(forKey: "business") as! NSObject
let categories = geo_business.value(forKey: "localizedCategories") as! [AnyObject]

var categoriesList = [Any]()

for geo_cat in categories.first as! [AnyObject] {

    let level = geo_cat.value(forKeyPath: "level") as! Int
    let geo_loc_name = geo_cat.value(forKeyPath: "localizedNames") as! NSObject
    let name = (geo_loc_name.value(forKeyPath: "name") as! [String]).first

    categoriesList.append(["level" : NSNumber(value: level), "name" : name as Any])

}

这为您提供了以下类别:

{

    level : 1;
    name : "Coffee & Tea"

}

1
投票
func retriveCategory(item:MKMapItem) -> [String] {
    let geo_place = item.value(forKey: "place") as! NSObject
    let geo_business = geo_place.value(forKey: "business") as! NSObject
    let categories = geo_business.value(forKey: "localizedCategories") as! [AnyObject]

    var categoriesList = [String]()

    if let listCategories = (categories.first as? [AnyObject]) {
        for geo_cat in listCategories {
            let geo_loc_name = geo_cat.value(forKeyPath: "localizedNames") as! NSObject
            let name = (geo_loc_name.value(forKeyPath: "name") as! [String]).first!

            categoriesList.append(name)
        }
    }

    return categoriesList
}

答案的另一个版本@Will Von Ullrich


0
投票

对于几年后仍在寻找的人来说,

MKMapItem
有一个
pointOfInterestCategory
属性

© www.soinside.com 2019 - 2024. All rights reserved.