将地址转换为经度和纬度

时间:2022-09-06 20:51:06

I want to do the following - Allow users to input a physical address in ios app. - This address is converted to longitude/latitude - Django server code checks to see nearby locations.

我想执行以下操作 - 允许用户在ios应用程序中输入物理地址。 - 此地址转换为经度/纬度 - Django服务器代码检查以查看附近的位置。

How can I do this most efficiently?

我怎样才能最有效地完成这项工作?

Should I convert the address to longitude and latitude in ios app and then send the coordinates to django server code, or should I send the address to django server code and then covert address to longitude and latitude?

我应该在ios app中将地址转换为经度和纬度,然后将坐标发送到django服务器代码,还是应该将地址发送到django服务器代码然后将地址转换为经度和纬度?

any help would be appreciated!

任何帮助,将不胜感激!

3 个解决方案

#1


5  

According to the Apple's Location Awareness Programming Guide You can achieve this using a CLGeocoder object:

根据Apple的位置感知编程指南您可以使用CLGeocoder对象实现此目的:

CLGeocoder* geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:@"Your Address"
     completionHandler:^(NSArray* placemarks, NSError* error){
         for (CLPlacemark* aPlacemark in placemarks)
         {
         // Process the placemark.
         }
}];

A CLPlacemark object has a property called location that yields latitude and longitude for the place.

CLPlacemark对象具有一个名为location的属性,可以为该位置生成纬度和经度。

#2


1  

You can use this function to get (latitude, longitude) tuple on the django side

您可以使用此功能获取django侧的(纬度,经度)元组

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

#3


0  

Use the below link,this will return json which consist of latitude and longitude for the physical address.

使用下面的链接,这将返回包含物理地址的纬度和经度的json。

http://maps.google.com/maps/api/geocode/json?address=Newyork&sensor=false

#1


5  

According to the Apple's Location Awareness Programming Guide You can achieve this using a CLGeocoder object:

根据Apple的位置感知编程指南您可以使用CLGeocoder对象实现此目的:

CLGeocoder* geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:@"Your Address"
     completionHandler:^(NSArray* placemarks, NSError* error){
         for (CLPlacemark* aPlacemark in placemarks)
         {
         // Process the placemark.
         }
}];

A CLPlacemark object has a property called location that yields latitude and longitude for the place.

CLPlacemark对象具有一个名为location的属性,可以为该位置生成纬度和经度。

#2


1  

You can use this function to get (latitude, longitude) tuple on the django side

您可以使用此功能获取django侧的(纬度,经度)元组

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

#3


0  

Use the below link,this will return json which consist of latitude and longitude for the physical address.

使用下面的链接,这将返回包含物理地址的纬度和经度的json。

http://maps.google.com/maps/api/geocode/json?address=Newyork&sensor=false