Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
PHP Lat / Lon Solution
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Crestien  
View profile  
 More options Jul 2 2005, 4:22 pm
From: "Crestien" <crest...@gmail.com>
Date: Sat, 02 Jul 2005 13:22:12 -0700
Local: Sat, Jul 2 2005 4:22 pm
Subject: PHP Lat / Lon Solution
I saw all the trouble people were having getting Lat and Lon
information and I couldn't get reliable results from geocoder.  So I
came up with this temporary solution.  I don't believe it's against
Google's Terms of Use.  Someone correct me if I'm wrong.

I just use this function to get the lat and lon and save it into my
database.  That way I'm not relying on this code to work forever.

I hope it helps someone out and doesn't make Google folks mad.

function getLatLongFromGoogle($q) {

  //Open Google Page
  //For some reason output=js gives a smaller page so we use it.
  $gm =
fopen('http://maps.google.com/maps?q='.urlencode($q).'&output=js','r');
  $tmp = @fread($gm, 30000);
  fclose($gm);

  //Get Lat
  $x   = eregi_replace('^.*lat="', '', $tmp);
  $lat = eregi_replace('".*$', '', $x);

  //Get Long
  $x   = eregi_replace('^.*lng="', '', $tmp);
  $lng = eregi_replace('".*$', '', $x);

  return(array($lat,$lng));

}

list($lat,$lng) = getLatLongFromGoogle('1600 Pennsylvania Ave NW, DC');

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Aaron Silvers  
View profile  
 More options Jul 4 2005, 5:28 pm
From: Aaron Silvers <aaron.silv...@gmail.com>
Date: Mon, 4 Jul 2005 17:28:34 -0400
Local: Mon, Jul 4 2005 5:28 pm
Subject: Re: PHP Lat / Lon Solution
Nor should it.  All you've done at this point is assign values to  
$lat and $lng.

You can echo them immediately after the list( $lat, $lng) and you'll  
get results.

Thanks so much, Cretien, for the code sample.  I've transcrbed it  
without using fopen, for those of us with webhosts that would rather  
one use cURL:

<?php

function getLatLngFromGoogle( $q )
{
     $ch = curl_init();
     $timeout = 5; //set to zero for no timeout
     curl_setopt( $ch, CURLOPT_URL, "http://maps.google.com/maps?
q=" . urlencode( $q ) . "&output=js" );
     curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
     curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
     $tmp = curl_exec( $ch );
     curl_close( $ch );

      //Get Lat
     $x   = eregi_replace('^.*lat="', '', $tmp);
     $lat = eregi_replace('".*$', '', $x);

     //Get Long
     $x   = eregi_replace('^.*lng="', '', $tmp);
     $lng = eregi_replace('".*$', '', $x);

     return( array( $lat, $lng ) );

}

list( $lat, $lng ) = getLatLngFromGoogle( '1021 S. State St. Chicago,  
IL 60605' );
echo "lat = $lat and lon = $lng";

?>

You can see that it works here:

http://beta.aaron21.com/curl_fileGetContents_test.php

-a-

On Jul 2, 2005, at 8:10 PM, Drace wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
nmwelch@gmail.com  
View profile  
 More options Jul 5 2005, 9:06 pm
From: "nmwe...@gmail.com" <nmwe...@gmail.com>
Date: Tue, 05 Jul 2005 18:06:56 -0700
Local: Tues, Jul 5 2005 9:06 pm
Subject: Re: PHP Lat / Lon Solution
I have taken your function listed and created a script to pull all of
the addresses listed in my database and pass in that address to google,
then I take the lat and long and insert those into the database row of
that address.

So now I have all the lats, and longs but I dont know how to pull the
lat and long from my DB and pass those PHP variables into a Javascript
function for the API.  Any suggestions would be helpful.

Thanks

Nathan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Crestien  
View profile  
 More options Jul 7 2005, 3:30 pm
From: "Crestien" <crest...@gmail.com>
Date: Thu, 07 Jul 2005 12:30:50 -0700
Local: Thurs, Jul 7 2005 3:30 pm
Subject: Re: PHP Lat / Lon Solution
I believe you should be able to open up a javascript page in an iframe,
which is generated with php, and simply set two javascript variables,
lat and lon, in that iframe.. which your main page can access.  That is
the no page refresh way of doing it.  Otherwise just echo the db
results into the javascript function within your page.

You can see an example of dynamic loading on my website
http://www.rentnear.com  I use this method to dynamically load selects
without a page refresh.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
b  
View profile  
 More options Jul 7 2005, 3:51 pm
From: "b" <boi...@gmail.com>
Date: Thu, 07 Jul 2005 12:51:15 -0700
Local: Thurs, Jul 7 2005 3:51 pm
Subject: Re: PHP Lat / Lon Solution
Here's a Cold Fusion (not PHP) snippet for getting geocodes from
Google. I assume it's fairly easy to figure out for whatever language
you're using. The snippet below parses the response from google,
locates the 'lat=' and 'lng=' pattern in the response string (via
regular expressions), then grabs each of the attributes based on their
position and length in the string (via the two arrays -- .pos and .len
-- returned by REFind. I assume PHP has something similar.)

My CF is a little rusty, so I'm not sure if I needed the <CFOUTPUT> in
the final two lines. But it works.

---

(the variable 'addy' here is the actual street address, city, zip --
something like "555 West Hampton, Boone, Iowa, 50601" or whatever)

<CFSET myURL="http://maps.google.com/maps?q=" & xmlformat(addy) &
"&output=js">

<cfsilent>
<cftry>
        <cfhttp method="get" timeout="15" url="#myURL#"></cfhttp>
<cfcatch></cfcatch></cftry>
</cfsilent>

<CFSET strLAT=REFindNoCase("lat=""(.*?)""",
cfhttp.filecontent,1,"TRUE")>
<CFSET strLNG=REFindNoCase("lng=""(.*?)""",
cfhttp.filecontent,1,"TRUE")>

<CFOUTPUT>

<CFSET myLAT=#Mid(cfhttp.filecontent, strLAT.pos[1], strLAT.len[1])#>
<CFSET myLNG=#Mid(cfhttp.filecontent, strLNG.pos[1], strLNG.len[1])#>

</CFOUTPUT>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ken Robinson  
View profile  
 More options Jul 7 2005, 3:54 pm
From: "Ken Robinson" <kenrb...@rbnsn.com>
Date: Thu, 07 Jul 2005 12:54:54 -0700
Local: Thurs, Jul 7 2005 3:54 pm
Subject: Re: PHP Lat / Lon Solution

nmwe...@gmail.com wrote:
> I have taken your function listed and created a script to pull all of
> the addresses listed in my database and pass in that address to google,
> then I take the lat and long and insert those into the database row of
> that address.

> So now I have all the lats, and longs but I dont know how to pull the
> lat and long from my DB and pass those PHP variables into a Javascript
> function for the API.  Any suggestions would be helpful.

Remember, PHP executes on the server and can write javascript script
that contains the values from PHP. For examples, you can do something
like this:

<?php

  $center_lat = 40.353;
  $center_long = -75.354;

?>
<html>
<head>
<script type="text/javascript">
        map.centerAndZoom(new GPoint(<? echo $center_long.','.$center_lat ?>),
mag);
</script>

The above is only an example, but you should be able to see what's
happening and go from there.

Ken


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »