带地址的 MKMapView

发布于 2024-09-12 17:52:27 字数 52 浏览 2 评论 0原文

有没有办法让 MKMapView 放置具有给定地址的图钉?不使用坐标

谢谢

is there a way to make the MKMapView place a pin with a given address? Without using the Coordinates

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吲‖鸣 2024-09-19 17:52:27

这是我在应用程序中使用的代码片段

-(CLLocationCoordinate2D) getLocationFromAddressString:(NSString*) addressStr {
    NSString *urlStr = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
                           [addressStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSString *locationStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlStr]];
    NSArray *items = [locationStr componentsSeparatedByString:@","];

    double lat = 0.0;
    double lon = 0.0;

    if([items count] >= 4 && [[items objectAtIndex:0] isEqualToString:@"200"]) {
        lat = [[items objectAtIndex:2] doubleValue];
        lon = [[items objectAtIndex:3] doubleValue];
    }
    else {
        NSLog(@"Address, %@ not found: Error %@",addressStr, [items objectAtIndex:0]);
    }
    CLLocationCoordinate2D location;
    location.latitude = lat;
    location.longitude = lon;

    return location;
}

here is a snippet of code I used in an application

-(CLLocationCoordinate2D) getLocationFromAddressString:(NSString*) addressStr {
    NSString *urlStr = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
                           [addressStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSString *locationStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlStr]];
    NSArray *items = [locationStr componentsSeparatedByString:@","];

    double lat = 0.0;
    double lon = 0.0;

    if([items count] >= 4 && [[items objectAtIndex:0] isEqualToString:@"200"]) {
        lat = [[items objectAtIndex:2] doubleValue];
        lon = [[items objectAtIndex:3] doubleValue];
    }
    else {
        NSLog(@"Address, %@ not found: Error %@",addressStr, [items objectAtIndex:0]);
    }
    CLLocationCoordinate2D location;
    location.latitude = lat;
    location.longitude = lon;

    return location;
}
挖个坑埋了你 2024-09-19 17:52:27

这是我在应用程序中使用的代码片段

[NSString stringWithContentsOfURL:] 目前已弃用。你必须使用:

NSString *locationStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlStr] encoding:NSUTF8StringEncoding error:nil];

here is a snippet of code I used in an application

[NSString stringWithContentsOfURL:] is deprecated for now. You have to use:

NSString *locationStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlStr] encoding:NSUTF8StringEncoding error:nil];
最后的乘客 2024-09-19 17:52:27

由于当您进行 Google 搜索时,此结果位于第一页,因此我认为提供比 Google 地理编码(有限)更新鲜的解决方案是件好事。最好使用 Apple 地理编码器:

NSString *location = @"your address";

CLGeocoder *geocoder = [CLGeocoder new];
[geocoder geocodeAddressString:location
             completionHandler:^(NSArray* placemarks, NSError* error){
                 if (error) {
                     NSLog(@"%@", error);
                 } else if ([placemarks count]) {
                     CLPlacemark *topResult = [placemarks firstObject];
                     MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];


                     MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(placemark.coordinate, 5000, 5000);

                     [self.mapView setRegion:region animated:YES];
                     [self.mapView addAnnotation:placemark];
                 }
             }];

Since this result is in the first page when you make a Google search, I think it's good to give a fresher solution than the Google geocoding (limited). It's better to use an Apple geocoder:

NSString *location = @"your address";

CLGeocoder *geocoder = [CLGeocoder new];
[geocoder geocodeAddressString:location
             completionHandler:^(NSArray* placemarks, NSError* error){
                 if (error) {
                     NSLog(@"%@", error);
                 } else if ([placemarks count]) {
                     CLPlacemark *topResult = [placemarks firstObject];
                     MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];


                     MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(placemark.coordinate, 5000, 5000);

                     [self.mapView setRegion:region animated:YES];
                     [self.mapView addAnnotation:placemark];
                 }
             }];
瞄了个咪的 2024-09-19 17:52:27

由于 Google 发布了新版本的地理编码 API (升级说明),Google 地图网址@Aaron Saunders 提供的不再有效。此外,响应现在是 jsonxml,因此我进行了一些更改以在接受的响应中解决此问题。

- (CLLocationCoordinate2D)locationFromAddressString:(NSString*)addressString {

    if (!addressString.length) {
        ALog(@"provided an empty 'addressStr'");
        return CLLocationCoordinate2DMake(0.0, 0.0);
    }

    NSString *urlStr = [NSString stringWithFormat:GOOGLE_MAPS_GEOCODING_URL_STR,
                    [addressString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSError *dataError;
    NSError *jsonError;
    NSData *responseData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]
                                             options:NSDataReadingMappedAlways
                                               error:&dataError];
    NSDictionary *responseBody = [NSJSONSerialization JSONObjectWithData:responseData
                                                             options:NSJSONReadingMutableContainers
                                                               error:&jsonError];

    if (!dataError && !jsonError && [responseBody[@"status"] isEqualToString:@"OK"]) {
        NSDictionary *coords = responseBody[@"results"][0][@"geometry"][@"location"];
        return CLLocationCoordinate2DMake([coords[@"lat"] doubleValue],
                                      [coords[@"lng"] doubleValue]);
    }
    else {
        ALog(@"Address [%@] not found. \nResponse [%@]. \nError [%@]",addressString, responseBody, jsonError);
        return CLLocationCoordinate2DMake(0.0, 0.0);
    }
}

Since google posted a new version to their geocoding API (upgrade notes), the Google Maps URL provided by @Aaron Saunders is no longer valid. Additionally, the response is now either json or xml so I have made some changes to address this in the accepted response.

- (CLLocationCoordinate2D)locationFromAddressString:(NSString*)addressString {

    if (!addressString.length) {
        ALog(@"provided an empty 'addressStr'");
        return CLLocationCoordinate2DMake(0.0, 0.0);
    }

    NSString *urlStr = [NSString stringWithFormat:GOOGLE_MAPS_GEOCODING_URL_STR,
                    [addressString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSError *dataError;
    NSError *jsonError;
    NSData *responseData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]
                                             options:NSDataReadingMappedAlways
                                               error:&dataError];
    NSDictionary *responseBody = [NSJSONSerialization JSONObjectWithData:responseData
                                                             options:NSJSONReadingMutableContainers
                                                               error:&jsonError];

    if (!dataError && !jsonError && [responseBody[@"status"] isEqualToString:@"OK"]) {
        NSDictionary *coords = responseBody[@"results"][0][@"geometry"][@"location"];
        return CLLocationCoordinate2DMake([coords[@"lat"] doubleValue],
                                      [coords[@"lng"] doubleValue]);
    }
    else {
        ALog(@"Address [%@] not found. \nResponse [%@]. \nError [%@]",addressString, responseBody, jsonError);
        return CLLocationCoordinate2DMake(0.0, 0.0);
    }
}
初懵 2024-09-19 17:52:27

您需要使用地理编码服务将您的地址转换为坐标。我认为谷歌提供了一项服务以及其他一些服务。

You need to use a geocoding service to convert your address to coordinates. Google, I think, offers one, along with a few other services.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文