Xcode iOS 4.2.1 ->给地图查看地址以显示图钉?

发布于 2024-10-13 05:38:55 字数 286 浏览 2 评论 0原文

可能的重复:
使用 CLGeocoder 的转发地理编码示例

我有一个充满姓名及其地址的 plist。我希望我的 iOS 应用程序在地图视图上显示一个带有他所选择的人的地址的图钉。我可以将地址导入到地图视图并获取图钉吗?又如何?谢谢。

Possible Duplicate:
Forward Geocode Example using CLGeocoder

I have a plist full of names and their adrresses. I want my iOS app to show on a map view a pin with the address of the person he has selected. Can I import the address to map view and get a pin? And how?? Thank you.

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

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

发布评论

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

评论(3

单挑你×的.吻 2024-10-20 05:38:55

使用 CLGeocoder 类将地址转换为纬度/经度坐标:

使用 CLGeocoder 的正向地理编码示例

您拥有的地址越准确,结果就越好,因此使用准确的地址,您应该获得非常准确的点。

Use the CLGeocoder class to convert addresses into lat/long coordinates:

Forward Geocode Example using CLGeocoder

The more accurate address you have, the better the result should be so with exact addresses you should get very accurate points.

郁金香雨 2024-10-20 05:38:55

另一种选择是使用 Google 的地理编码网络服务,只需将地址字符串传递给此函数,您将获得一个 CLLocationCooperative2D,其中包含纬度和坐标。经度。

现在它获取第 0 个索引的位置,这是最接近的匹配结果。注销结果以查看来自 Google 的 JSON 响应。

- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
    NSString *esc_addr =  [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=true&address=%@", esc_addr];
    CLLocationCoordinate2D center;


    NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
    if (result)
    {
        //NSLog(@"LOC RESULT: %@", result);
        NSError *e;
        NSDictionary *resultDict = [NSJSONSerialization JSONObjectWithData: [result dataUsingEncoding:NSUTF8StringEncoding]
                                                                   options: NSJSONReadingMutableContainers
                                                                     error: &e];
        NSArray *resultsArray = [resultDict objectForKey:@"results"];

        if(resultsArray.count > 0)
        {
            resultDict = [[resultsArray objectAtIndex:0] objectForKey:@"geometry"];
            resultDict = [resultDict objectForKey:@"location"];
            center.latitude = [[resultDict objectForKey:@"lat"] floatValue];
            center.longitude = [[resultDict objectForKey:@"lng"] floatValue];
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Results Found" message:@"No locations were found using, please try again." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
            [alert show];
        }

        //goes through each result
        /*for(NSDictionary *dict in resultsArray)
        {
            resultDict = [dict objectForKey:@"geometry"];
            resultDict = [resultDict objectForKey:@"location"];
        }*/
    }
    return center;
}

Another option is to use Google's geocoding web service, simply pass the address string to this function and you will get an CLLocationCoordinate2D which contains a latitude & longitude.

Right now it grabs the location at the 0th index, which is the closest matching result. Log out the result to see the JSON response from Google.

- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
    NSString *esc_addr =  [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=true&address=%@", esc_addr];
    CLLocationCoordinate2D center;


    NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
    if (result)
    {
        //NSLog(@"LOC RESULT: %@", result);
        NSError *e;
        NSDictionary *resultDict = [NSJSONSerialization JSONObjectWithData: [result dataUsingEncoding:NSUTF8StringEncoding]
                                                                   options: NSJSONReadingMutableContainers
                                                                     error: &e];
        NSArray *resultsArray = [resultDict objectForKey:@"results"];

        if(resultsArray.count > 0)
        {
            resultDict = [[resultsArray objectAtIndex:0] objectForKey:@"geometry"];
            resultDict = [resultDict objectForKey:@"location"];
            center.latitude = [[resultDict objectForKey:@"lat"] floatValue];
            center.longitude = [[resultDict objectForKey:@"lng"] floatValue];
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Results Found" message:@"No locations were found using, please try again." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
            [alert show];
        }

        //goes through each result
        /*for(NSDictionary *dict in resultsArray)
        {
            resultDict = [dict objectForKey:@"geometry"];
            resultDict = [resultDict objectForKey:@"location"];
        }*/
    }
    return center;
}
二手情话 2024-10-20 05:38:55

这就是所谓的正向地理编码,并且没有内置 API 可以为您执行此操作。您需要使用外部服务,使用哪一个实际上取决于您的应用程序。

我为此使用了谷歌的服务。 http://code.google.com/apis/maps/documentation/geocoding/

这是一个非常简单的 API,但根据您的应用程序许可证有限制。我知道还有一些其他服务可以实现此目的,但我将其作为练习留给您。

This is whats known as Forward Geocoding, and there is no built in API to do this for you. You need to use an external service, which one you use really depends on your app.

I have used Google's service for this. http://code.google.com/apis/maps/documentation/geocoding/

It is a very simple API, but has restrictions based on your app's license. I know there are a couple other services for this, but I'll leave that as an exercise to you.

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