我想做以下事情 - 允许用户在ios应用程序中输入物理地址。 - 此地址转换为经度/纬度 - Django服务器代码检查以查看附近的位置。
如何最有效地完成这项工作?
我应该在ios app中将地址转换为经度和纬度,然后将坐标发送到django服务器代码,还是应该将地址发送到django服务器代码然后将地址转换为经度和纬度?
任何帮助将不胜感激!
答案 0 :(得分:5)
根据Apple的Location Awareness Programming Guide您可以使用CLGeocoder对象实现此目的:
CLGeocoder* geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:@"Your Address"
completionHandler:^(NSArray* placemarks, NSError* error){
for (CLPlacemark* aPlacemark in placemarks)
{
// Process the placemark.
}
}];
CLPlacemark对象具有一个名为location的属性,可以为该位置生成纬度和经度。
答案 1 :(得分:1)
您可以使用此功能在django端获取(latitude, longitude)
元组
import urllib, urllib2
import simplejson as json
def getLatLng( address ):
""" Native address format is House Number, Street Direction, Street Name,
Street Suffix, City, State, Zip, Country """
TIMEOUT = 3
try:
url = "http://maps.google.com/maps/api/geocode/json?address=" + urllib.quote_plus( address.encode('utf-8') ) + "&sensor=false"
opener = urllib2.build_opener()
req = urllib2.Request( url )
data = json.load( opener.open(req, None, TIMEOUT) )
results = data['results']
if len( results ):
location = results[0]['geometry']['location']
return ( location['lat'], location['lng'] )
else:
return None
except:
return None
答案 2 :(得分:0)
使用以下链接,这将返回json,其中包含物理地址的纬度和经度。
http://maps.google.com/maps/api/geocode/json?address=Newyork&sensor=false