How to get Google Map coordinates by address ?

ardas - August 28, 2006 - 15:13

Greetings,

I've installed profile.module, gmap.module, gmap_location.module, I have Country and City fields in user's profile and I need to get GMap coordinates by these fields ... So, I don't want user to go to Location map page and enter his address to get coordinates I would like to do it automatically when user saves his profile information.

So, I want his Country and City fields concatinated together to be used as google address to receive coordinates and save them to location table. How can I do that ?

Thanks.

Similar needs

hitesh pherwani - September 25, 2006 - 12:12

Hello,

Even I want something similar.

Regards,

Hitesh

I will also want this

sagarkhetre - May 10, 2008 - 07:57

I will also want this information. want any tool for that

+1 for this feature request

Summit - May 10, 2008 - 09:44

+1 for this feature request also on Drupal 5, also on cck_map and location module
Greetings, Martijn

coordinates for a city

mwease - August 23, 2009 - 20:17

did you figure this out? i want to use the geonames module but i can't figure out how to get the coordinates (we're talking lattitude and longitude, right?) using the geonames_query PHP function. well, i get them, but i can't determine within PHP WHICH city by the same name of the 15 (in one case) returned is the one i want. the data returned for the cities does NOT contain it's parent (state) or the state's parent (country). there must be a way, but i can't figure it out. is somebody knows, i would love to hear it.

anyway, what i had done before i knew about geonames was create a PHP array for the coordinates. it's a file of over 30,000 records, one assignment for every city in the US and the bordering Canadian provinces/states.

example: $x = $citygps["OH"]["Columbus"]["coords"];
/* $x will be assigned the string "39.986,-82.988" */

it's up to more PHP code to extract the lattitude and longitude from that string (lattidude is first).

if you're interested, let me know. if i could get geonames to give me the coordinates, i wouldn't have to user this huge file, which is approx 1.8 meg.

Folks, Some of us noob are

trumanCodes1 - September 29, 2009 - 05:08

Folks,

Some of us noob are still interested in getting some more geonames examples. Please help. MWease if you have figured this out please post back and let us know how you solved the coordinates issue.

Truman Q
Area-Codes1.com

coordinates for geonames

mwease - September 29, 2009 - 19:43

here's what i've found out. the admin1code argument/parameter specifies the state (otherwise known as the first administrative entity below the country or some such jargon).

"$options" in the code below was meant to specify that i wanted returned name, lat and longitude. however, i just realized that i named the variable in an include file $queryoptions. therefore $options has been NULL for a month or more with no indication of anything wrong that i've noticed. i'm now afraid to remove it! :)

in my application, i'm dealing with the united states mostly, but also with the bordering provinces/states in canada. therefore, if i don't find the city in the US (i don't have the country at the time and i didn't bother to see if the "state" is one of those provinces, although that might be a little more efficient), l look for it canada with another query.

so, in the code below, i build the querry for the US, send it and get the results.

if they're NULL, i change the country parm to CA (canada). geoadmin1 is an array i set up for the canadian "states". they're numbered "01" through however many there are, whereas the admincode1 parms for the US states are actually the postal abbreviation.

if i don't find it in canada, bad news.

go here http://download.geonames.org/export/dump/admin1Codes.txt for a list of admincode1s for all countries (i assume).

i found an algorithm for computing distance between cities (which is what i needed) after two successful queries (i.e., two sets of lat and long) and translated it to PHP. if anyone wants that and my state abbreviations/names/geonames arrays include file, let me know. send me an email because i don't respond quickly to drupal.org posts. this was a coincidence. click on the clickable "mwease" and you can send an email from where that takes you. i hope it works. i've tried it twice for maintainers of modules, but have yet to receive a response from them. if you try it and you don't get a response from me within a day, it didn't work. they, you'll have to post something here.

i have found that this will return more than one city within a state (never thought THAT would be the case, but there are 3 (count 'em!) Mount Joys in PA), but i just take the first one because my user rep
said that was OK.

so far, the only cities i have NOT found this way (the miles="??") have ALWAYS been because the input city name was spelled wrong.

this is the code for the first city (repeat for another city, different variables or variable values, of course:

$query = array('name' => $cityname,'country' => 'US',
'admincode1' => $statenameabbreviation);
$result = geonames_query('search',$query,$options);
if ($result->results == NULL) /* couldn't find the city?!! canada? */
{
$query = array('name' => $cityname,
'country' => 'CA',
'admincode1' => $geoadmin1[$statenameabbreviation]);
$result = geonames_query('search',$query,$options);
}
if ($result->results == NULL) /* invalid radius city */
{
$miles = "(??)";
$badcity = $cityname . "," . $statenameabbreviation;
}
else
{
$lat1 = $result->results[0]["lat"];
$long1 = $result->results[0]["lng"];
}

 
 

Drupal is a registered trademark of Dries Buytaert.