Google Maps API

LotsOfZeros

^^^ Bi-Winning ^^^
Feb 9, 2008
4,649
118
0
www.makemoniesonline.com
Most scripts and tuts I am locating are either outdated or irrelevant to what I'm trying to accomplish. A certain directory site I am working on queries my database and returns addresses which I will now have to somehow convert into Lat/Long for Google Maps API.

Somehow the value needs to get in the GLatLng object which is what I am having an issue wrapping my head around - I suck at javascript.

Ideas, links, tutorials and assistance is appreciated.
 


The ideal way and the way I do it is to store the Lat/Long in the database so u don't have to do it on the fly. Only way to do it on the fly would be behind the scenes in php/asp.net by querying one of several sites that will convert it for you. This takes time to do, so its better to do it just once and store in the DB so u dont have to do it each time
 
This is off a ASP.net script I used to get the position by zip code. You can likely mod it to work for your needs too:


[high=language]

Public Structure structInfo
Dim ID As Integer
Dim City As String
Dim State As String
Dim Country As String
Dim Err As Boolean
Dim ErrCode As String
Dim Zip As String
Dim Longitude As Decimal
Dim Latitude As Decimal
Dim Altitude As Decimal
End Structure

Private Function Get_Site(ByVal Zip As String, ByVal API As String) As Boolean

Dim client As New System.Net.WebClient()

' Add a user agent header in case the
' requested URI contains a query.
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705")

Dim data As System.IO.Stream = client.OpenRead("http://maps.google.com/maps/geo?" & _
"output=kml&oe=utf-8&q=" & System.Web.HttpUtility.UrlEncode(Zip) & "&" & _
"key=" & System.Web.HttpUtility.UrlEncode(API))
Dim reader As New System.IO.StreamReader(data)
Dim s As String = reader.ReadToEnd()
data.Close()
reader.Close()


'If we didnt get the correct info return false
If Not s.StartsWith("<?xml version=""1.0""") Then Return False


Try

'Now set the data as a XML
Dim XML As New Xml.XmlDocument()
XML.LoadXml(s)
'Make sure theres no error
Dim frames As Xml.XmlNodeList = XML.GetElementsByTagName("code")
Dim temp As String = frames(0).InnerText

With Info

.ErrCode = temp
If (.ErrCode <> "200") Then
.Err = True
Return False
End If

If InStr(s, "LocalityName") Then .City = XML.GetElementsByTagName("LocalityName")(0).InnerText Else .City = "Unknown"
If InStr(s, "CountryNameCode") Then .Country = XML.GetElementsByTagName("CountryNameCode")(0).InnerText Else .Country = "NA"
If InStr(s, "AdministrativeAreaName") Then .State = XML.GetElementsByTagName("AdministrativeAreaName")(0).InnerText Else .State = "Unknown"
.Zip = XML.GetElementsByTagName("PostalCodeNumber")(0).InnerText

Dim sSecs() As String = Split(XML.GetElementsByTagName("coordinates")(0).InnerText, ",")

.Latitude = CDec(sSecs(1))
.Longitude = CDec(sSecs(0))
.Altitude = CDec(sSecs(2))
End With

Return True

Catch ex As Exception

Info.Err = True
Return False

End Try


End Function



[/high]
 
The ideal way and the way I do it is to store the Lat/Long in the database so u don't have to do it on the fly. Only way to do it on the fly would be behind the scenes in php/asp.net by querying one of several sites that will convert it for you. This takes time to do, so its better to do it just once and store in the DB so u dont have to do it each time


Right, after you figure out how to get the lat and long store them.. but for now Pull your addresses out and put them into an array for JS.. something like:

Code:
        var data = new Array();

        //Loop though Table and set address array
        <? foreach ( $data as $k=>$v ) : ?>        
        data[<?=$k?>] = "<?=$v['address'].', '.$v['city'].', '.$v['state'];?>";
        <? endforeach; ?>
Then loop through this in JS and set markers.
Code:
    // adds markers to the map
    function showAddress(address, i) {
      geocoder.getLatLng(
        address,
        function(point) {
          if (!point) {
             $('#error').append(address+'<br />');
          } else {
            var marker = new GMarker(point);
            map.addOverlay(marker);
            
            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(getHtml(i));
              });
            
          }
        }
      );
    }

   //Loops though the "data" address array and sends 
   //each address to the showAddress function.
  // waits so Google dosent hate you 
    var c = 1;
    function loopCalls() {
        c += 1;
        if ( c <= data.length) {
            showAddress(data[c], c);
        }        
        setTimeout ("loopCalls()", 500);
    }
 
What are the reliable and accurate services to use for getting the lon/lat on these addresses? I'm thinking of using a script to run through my database and submit the address while capturing the return data and storing it in my db.

I'm doing this in php.