6

MapKit を利用するアプリケーションを作成しています。MKLocalSearch を実装し、MKMapItem の配列を取得しました。しかし、これらの各項目のカテゴリを取得できるかどうか疑問に思っていました。たとえば、マップ アプリケーションでは、ショップ、ホテル、駅などにさまざまなアイコンが表示されます。また、目印を表示すると、食料品などのカテゴリ ラベルが表示されます。開発者として、マップ アイテムのその情報にアクセスできますか? もしそうなら、私はその方法を知りたいです。

ありがとうございました

4

3 に答える 3

4

はい、この情報を取得できます。検索場所からの情報の詳細については、以下の方法を参照してください。

残念ながら、住所の詳細は からしか取得できませんMKPlacemark

今あなたがしなければならないことは、住所の詳細を取得するMKPlacemarkことです。住所をいくつかのラベル/注釈に分類するのに役立つオープンソース API の助けを借りる必要があります。

優れた API の 1 つに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)
            }
        }

    }
}
于 2014-12-15T05:45:52.800 に答える