如何从地址字符串获取经纬度坐标 [英] How to get lat and long coordinates from address string

查看:275
本文介绍了如何从地址字符串获取经纬度坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MKMapView,它的顶部有一个UISearchBar,我希望用户能够键入一个地址,并找到该地址并在其上放一个针脚.我不知道如何将地址字符串转换为经度和纬度,因此我可以创建一个CLLocation对象.有人知道我该怎么做吗?

I have a MKMapView that has a UISearchBar on the top, and I want the user to be able to type a address, and to find that address and drop a pin on it. What I don't know is how to turn the address string into longitude and latitude, so I can make a CLLocation object. Does anyone know how I can do this?

推荐答案

您可能会在此问题中找到答案.

You may find your answer in this question.

iOS-通过使用MKMapView位置注释地址,而不是经度/长由用户Romes.

NSString *location = @"some address, state, and zip";
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder geocodeAddressString:location 
             completionHandler:^(NSArray* placemarks, NSError* error){
                 if (placemarks && placemarks.count > 0) {
                     CLPlacemark *topResult = [placemarks objectAtIndex:0];
                     MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];

                     MKCoordinateRegion region = self.mapView.region;
                     region.center = placemark.region.center;
                     region.span.longitudeDelta /= 8.0;
                     region.span.latitudeDelta /= 8.0;

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

一个非常简单的解决方案.但仅适用于iOS5.1或更高版本.

A Very simple solution. But only applicable on iOS5.1 or later.

这篇关于如何从地址字符串获取经纬度坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆