0

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.

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?

any help would be appreciated!

user2237822
  • 605
  • 2
  • 10
  • 13
  • Have a look at [GeoDjango](https://docs.djangoproject.com/en/dev/ref/contrib/gis/) - it enables you to do geo spatial lookups... For converting the address to coordinates you will most like need an external API. – Bernhard Vallant May 22 '13 at 14:28
  • I'm using this javascript and it works just fine: https://github.com/ubilabs/geocomplete/#populate-form-data – Ronaldo Bahia Mar 14 '16 at 17:10

3 Answers3

5

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

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.

micantox
  • 5,446
  • 2
  • 23
  • 27
1

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

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
Pawel Furmaniak
  • 4,648
  • 3
  • 29
  • 33
0

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

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

Mage
  • 151
  • 1
  • 14