Jade Sayo
Jade Sayo

Reputation: 35

How to acquire Longitude and Latitude via Code

I'm asked to locate the Longitude and Latitude of a specific place. The user will give the address of the location. How may I get the Longitude and Latitude? Any suggestions are greatly appreciated.

Upvotes: 3

Views: 132

Answers (2)

deniskoronets
deniskoronets

Reputation: 540

I'm use google api for get geo-coordinates from address in my project:

class Google
{
    private function __construct() { }

    public static function getGoogleJson($address)
    {
        $apiLink = 'http://maps.google.com/maps/api/geocode/json?sensor=false&language=ru&address=' . urlencode($address);

        $tmp = @json_decode(file_get_contents($apiLink));

        if (!$tmp) {
            return new stdClass;
        } else {
            return $tmp;
        }
    }

    public static function getGeoByAddress($address)
    {
        $addr = self::getGoogleJson($address);

        if (isset($addr->results[0]->geometry->location)) {
            return (array)$addr->results[0]->geometry->location;
        } else {
            return ['lat' => 0, 'lng' => 0];
        }
    }
}

(ACHTUNG! Google have some limit for use this api per day)

Upvotes: 1

Steve Gray
Steve Gray

Reputation: 460

You'll need to perform what's known as Geocoding in order to translate the address text into a pair of lat/lon coordinates. Generally the good services for this have some sort of quota or paid subscription model, but you can find some free API's at:

http://www.programmableweb.com/news/7-free-geocoding-apis-google-bing-yahoo-and-mapquest/2012/06/21

Further Google-work on your part will find other providers, as I've supplied the key term: geocoding. Many have Javascript, PHP, C# and other implementation examples to help you get started.

Upvotes: 1

Related Questions