How to get longitude and latitude using code
2 answers
You will need to do something called geocoding to translate the text of the address into a lat / lon coordinate pair. Typically, good services for this have some sort of quota or paid subscription model, but you can find a free API at:
http://www.programmableweb.com/news/7-free-geocoding-apis-google-bing-yahoo-and-mapquest/2012/06/21
Further work by Google on your part will find other providers as I put in the key term: geocoding. Many of them have Javascript, PHP, C # and other implementation examples to help you get started.
+1
source to share
I am using google api to get geo-coordinates from an 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 has some limit for using this api per day)
+1
source to share