【问题标题】:How to search for a destination using name in MapKit?如何使用 MapKit 中的名称搜索目的地?
【发布时间】:2015-04-28 04:08:17
【问题描述】:

我想使用 MapKit 中的名称搜索目的地并返回经度和纬度 (CLLocationCoordinate2D)。

目前我正在使用硬编码值来设置目的地。

    // Make a directions request
    MKDirectionsRequest *directionsRequest = [MKDirectionsRequest new];

    // Start at our current location
    MKMapItem *source = [MKMapItem mapItemForCurrentLocation];
    [directionsRequest setSource:source];

    // Make the destination --> I WANT TO GET THIS COORDINATE USING NAME
    CLLocationCoordinate2D destinationCoords = CLLocationCoordinate2DMake(45.545824, 9.327515);
    MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:destinationCoords addressDictionary:nil];
    MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark:destinationPlacemark];
    [directionsRequest setDestination:destination];

【问题讨论】:

标签: ios objective-c mapkit


【解决方案1】:

您必须使用CLGeocoderforward-geocode 使用地址:

CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:@"New York City" completionHandler:^(NSArray *placemarks, NSError *error) {
    if (error) {
        NSLog(@"%@", error);
    } else {
        CLPlacemark *placemark = [placemarks lastObject];
        MKCoordinateRegion region;
        region.center.latitude = placemark.location.coordinate.latitude;
        region.center.longitude = placemark.location.coordinate.longitude;
        [self.mapView setRegion:region animated:YES];
    }
}];

这会将地图的区域设置为返回的位置,使用纬度和经度作为中心。

【讨论】:

  • 谢谢,约翰·罗杰斯。知道了。但是,搜索结果不如 google 提供的强大...有些搜索关键字会返回空地标。
  • Sam Vermette 创建了一个使用 Google 地理编码 API 的地理编码器。在这里查看:github.com/TransitApp/SVGeocoder
猜你喜欢
  • 1970-01-01
  • 2014-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-12
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
相关资源
最近更新 更多