Sign In/My Account | View Cart  
advertisement

Article:
 Hacking Maps with the Google Maps API
Subject: Geocoding
Date: 2007-08-15 07:48:49
From: navyjax2

Actually, Google maps does include a geocoding ability (see the area just above where this link will take you -http://www.google.com/apis/maps/documentation/#Geocoding_Examples). Only thing is, I've found that it doesn't seem to play nicely with XML. I could plot points from XML addresses fine, but because it pulls the points asynchrously, I'm finding it hard to match XML data (name, description) to a point when the marker gets displayed in order to accurately and reliably display its information on an infoWindow. I can do it fine for lat/lng, but not for addresses. Was hoping someone had messed around with this...


Here are the applicable snippets of code:


function onload() {
GDownloadUrl("dataptsaddr.xml", function(data) {
var xml = GXml.parse(data);
var addresses = xml.documentElement.getElementsByTagName("wpt");


// loop through the XML addresses
for (var i = 0; i < (addresses.length); i++) {
var xmlAddress = addresses[i].getAttribute("addr");
geocoder.getLatLng(
xmlAddress,
new Function("point", "writeMarker(point, '" + xmlAddress + "');")
);
}
});
}


function writeMarker(point, xmlAddress) {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
map.addControl(new GScaleControl());
map.enableScrollWheelZoom();


thepointbox.value = point;
theaddrbox.value = xmlAddress;



GDownloadUrl("dataptsaddr.xml", function(data) {


var xml = GXml.parse(data);
var addresses = xml.documentElement.getElementsByTagName("wpt");
var point = thepointbox.value;
var address = theaddrbox.value;
var j = theindexbox.value;
var name = xml.documentElement.getElementsByTagName("name")[j].childNodes(0).nodeValue;
var desc = xml.documentElement.getElementsByTagName("desc")[j].childNodes(0).nodeValue;
// alert(j + ' ' + point + ' ' + address + ' ' + name + ' ' + desc);
// var marker = createMarker(point,address,"<div id=\"gmapmarker\">" + name + "
" + desc + "</div>", 2);
var marker = new GMarker(point);
map.addOverlay(marker);
marker.openInfoWindowHtml('' + name + '
' + desc + '
' + point + '' );
if (theindexbox.value < addresses.length) {theindexbox.value++;}
alert(j);
});
} else {alert("Sorry, the Google Maps API is not compatible with this browser.");}
}


"Wpt" is my leading node's tag, and "Addr" is the tag for the address in each "wpt" section. I left the "alert(j)" to demonstrate that, using textboxes to feed in revised values into Google's XML parsing function (GDownloadUrl), I can increase the index effectively to get the next XML data as new points and xmlAddresses are fed from the geocoder, but still no marker ever gets displayed. I had to pass values between the functions using textboxes (thepointbox, theindexbox, etc.) because global variables don't seem to work for callback functions - would always show as undefined without doing it this way. When I use the same writeMarker function with purely GPS points, it works perfectly. I've done an alert on point, xmlAddress coming into writeMarker from the geocoder and my index, name, and description fields from the XML and all are correct, they just won't display using var marker = GMarker(point) and map.addOverlay(marker), (where var map = new GMap2(document.getElementById("map"));) even though I used this with lat/lng points and it's worked fine.... Just thought I'd put all this out there in case someone wanted to play with it.


Previous Message Previous Message   Next Message No Next Message


Titles Only Titles Only Newest First
  • Geocoding
    2007-08-15 07:53:52 navyjax2

    Here's some sample XML data, btw:


    <?xml version="1.0" standalone="yes"?>
    <gpx version="1.0" creator="GPS Visualizer http://www.gpsvisualizer.com/" xmlns="http://www.topografix.com/GPX/1/0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
    <wpt addr="39.15497, -77.51940">
    <name>White's Ferry</name>
    <desc>24801 White's Ferry Rd, Dickerson, MD 20842</desc>
    <state>MD</state>
    </wpt>
    </gpx>


    -Tom

    • Geocoding
      2007-08-15 08:06:50 navyjax2

      Actually, I shouldn't have used that section of XML - I meant to post something that had a normal address that worked on a Google map, instead of one that included the coordinate points already. I had that other section in there because I wanted the code able to do both (it doesn't matter to Geocoder whether it's getting a GPS point or an address - it still spits back a GPS point to plot). Here's a better example section:


      <wpt addr="150 Malin Dr, Wytheville, VA, 24382">
      <name>Days Inn</name>
      <desc>150 Malin Dr, Wytheville, VA 24382</desc>
      <state>VA</state>
      </wpt>


Sponsored By: