ramone_johnny Posted May 5, 2013 Share Posted May 5, 2013 (edited) Hey guys, I have code here written in XML/ASP that I need some help with. Its a pop window that launches a Google map, that allows users to "verify" the entered address. I have the values passing okay to the popup window, I'm just lost as to how I can convert this page to PHP. Is there anyone here that is knowledgeable with Google maps? I should probably mention that this script shows numerous markers when an exact match isn't found, allowing the user to "click" on the right marker, which repopulates the parent form (with the correct address) <!-- #INCLUDE VIRTUAL="/includes/adovbs.inc" --> <!-- #INCLUDE VIRTUAL="/includes/declarations.asp" --> <!-- #INCLUDE VIRTUAL="/includes/functions.asp" --> <html> <head> <meta name="robots" content="noindex,nofollow"> <link rel="SHORTCUT ICON" href="http://www.bleh.com/web.ico"/> <title>Website | Verify Address</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link rel="stylesheet" type="text/css" href="/includes/white.css"> <br> <? $num = $_REQUEST["num"] ; $street = $_REQUEST["street"] ; $suburb = $_REQUEST["suburb"] ; $state = $_REQUEST["state"] ; $pcode = $_REQUEST["pcode"] ; $pcode_asis = $pcode ; echo "the number is : " . $num . " : " . "<br>" ; echo "the street is : " . $street . " : " . "<br>" ; echo "the suburb is : " . $suburb . " : " . "<br>" ; echo "the state is : " . $state . " : " . "<br>" ; echo "the pcode is : " . $pcode . " : " . "<br><hr>" ; exit() ; ?> <% CONST_ZOOM = 15 ; //' higher number means closer in zoom // This is the magic url with magic google key: baseURL = "http://maps.google.com/maps/geo?output=xml&key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ; // so do we have any addresses to display? // let's find out... //' Master array to hold the info returned from the geocode query addresses( ; foundAddrMax = -1 ; matchPost = 0 ; //' will match on first found location unless overridden //Dim num, street, suburb, city, state, pcode //' First, define the function to be called by the mainline code to test addresses: Function TryAddress( addr ) //' try to geocode this address URL = baseURL & Escape(addr) //' DEBUG: Response.Write "trying [" & URL & "]<br>" & vbNewLine Set http = Server.CreateObject("MSXML2.ServerXMLHTTP") http.open "GET", URL http.send Set xml = Server.CreateObject("Microsoft.XMLDom") xml.LoadXML http.ResponseText Set places = xml.getElementsByTagName("Placemark") If places Is Nothing Then TryAddress = False Exit Function //' oops End If If places.Length = 0 Then TryAddress = False Exit Function //' oops End If foundAddrMax = places.Length - 1 //' how many found? If foundAddrMax > 8 Then foundAddrMax = 8 //' but no more than 9 For anum = 0 To foundAddrMax //' and loop through each Set place = places(anum) Set xmlAddr = place.getElementsByTagName("address") Set xmlSuburb = place.getElementsByTagName("LocalityName") Set xmlStreet = place.getElementsByTagName("ThoroughfareName") Set xmlPcode = place.getElementsByTagName("PostalCodeNumber") Set xmlLoc = place.getElementsByTagName("coordinates") //' we MUST find each one of those for an address to be legit If xmlSuburb Is Nothing OR xmlPcode Is Nothing OR xmlLoc Is Nothing Then // ' DEBUG: Response.Write "Abort on NOTHING<br>" TryAddress = False Exit Function //' oops End If // ' and there must be a value in AT LEAST PCode and coordinates for it to be legit If xmlPcode.Length = 0 OR xmlLoc.Length = 0 Then // ' DEBUG: Response.Write "Abort on LENGTH<br>" TryAddress = False Exit Function //' oops End If // ' order of elements in sub array is address, lat, long, street, suburb, pcode aStreet = "" : If xmlStreet.length > 0 Then aStreet = xmlStreet(0).Text aSuburb = "" : If xmlSuburb.length > 0 Then aSuburb = xmlSuburb(0).Text aAddr = "" : If xmlAddr.length > 0 Then aAddr = xmlAddr(0).Text aPCode = xmlPcode(0).Text //' already tested for temp = Split( xmlLoc(0).Text, "," ) //' google gives long, lat, unknown aLong = CDbl( Trim( temp(0) ) ) aLat = CDbl( Trim( temp(1) ) ) //' order of elements in sub array is address, lat, long, street, suburb, pcode addresses(anum) = Array( aAddr, aLat, aLong, aStreet, aSuburb, aPCode ) //' the "best" result is the one that matches the given postcode If aPcode = pcode Then matchPost = anum Next On Error Resume Next xml.Close On Error GoTo 0 TryAddress = True End Function //' back to mainline code //' we test various combos of those values looking for google to find a match //' first of all, street and number are always used: street = num & " " & street & ", " //' first attempt is all those values: address = street & suburb & ", " & city & ", " & state & ", " & pcode & " AUSTRALIA" //'Response.Write "<hr><br>" & address & "<hr>" & vbNewLine //' Response.Flush okay = TryAddress( address ) If Not okay Then //' try omitting the street (legal to omit for this usage) address = suburb & " " & city & ", " & state & ", " & pcode & " AUSTRALIA" okay = TryAddress( address ) End If If Not okay Then //' try omitting the city (legal to omit for this usage) address = street & suburb & ", " & state & ", " & pcode & " AUSTRALIA" okay = TryAddress( address ) End If okay = TryAddress( address ) If Not okay Then //' try omitting the suburb address = street & city & ", " & state & ", " & pcode & " AUSTRALIA" okay = TryAddress( address ) End If If Not okay Then //' assume postcode is bogus? address = street & suburb & " " & city & ", " & state & ", AUSTRALIA" okay = TryAddress( address ) End If If Not okay Then foundAddrMax = -1 //' safety measure! //'Response.Write "<hr><br>The address found is : " & address & "<hr>" & vbNewLine //' so if we found any good addresses, generate the map //' If foundAddrMax >= 0 Then //' move the best match to entry zero, so it shows at top of list If matchPost > 0 Then // ' swap the best match with number 0 temp = addresses(0) addresses(0) = addresses(matchPost) addresses(matchPost) = temp End If //' we center on the best address given (if any) //'Response.Write foundAddrMax & ", " & typename(addresses) & ", " & typename(addresses(0)) & "<HR>" //'Response.Write Join(addresses(0),"::<br>") & "<HR>" centerLat = addresses(0)(1) centerLong = addresses(0)(2) ?> <script src="http://maps.google.com/maps?file=api&v=2&key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" type="text/javascript"></script> <script type="text/javascript"> // copy the latitude and longitude arrays here, for the approval process: var latitudes = new Array( ); var longitudes = new Array( ); var mapOkay = false; var markers = new Array(); var googleMap = null; function initMap() { if ( !GBrowserIsCompatible()) { alert("Sorry! Your ancient browser doesn't support Google Maps!"); return; } var map = new GMap2(document.getElementById("GoogleMap")); map.setCenter(new GLatLng(<%=centerLat%>,<%=centerLong%>), <%=ZOOM%>); map.addControl(new GLargeMapControl()); map.addControl(new GMapTypeControl()); mapOkay = true; googleMap = map; // copy to global! <% ' now we generate as many points as needed For anum = 0 To foundAddrMax row = addresses(anum) ' order of elements in sub array is address, lat, long, street, suburb, pcode addr = row(0) latitude = row(1) longitude = row(2) %> latitudes[<%=anum%>] = <%=latitude%>; longitudes[<%=anum%>] = <%=longitude%>; var icon = new GIcon(); icon.image = "/images/marker<%=(anum+1)%>.png" icon.shadow = "/images/shadow-marker1.png"; icon.iconSize = new GSize(16.0, 33.0); icon.shadowSize = new GSize(33.0, 33.0); icon.iconAnchor = new GPoint(8.0, 30.0); icon.infoWindowAnchor = new GPoint(8.0, 16.0); //icon.iconSize = new GSize(22, 35); /// (w,h) //icon.iconAnchor = new GPoint(11, 36); // (w,h) h=increase to get pin point higher, 20 moves north 20 pixals //icon.infoWindowAnchor = new GPoint(11, 15); // Create a marker whose info window displays its latitude and longitude markers[<%=anum%>] = new GMarker(new GLatLng(<%=latitude%>, <%=longitude%>), icon); // Show info about this marker when it is clicked GEvent.addListener(markers[<%=anum%>], "click", function() { markers[<%=anum%>].openInfoWindowHtml("Address:<br/><%=addr%>"); } ); // and add this marker: map.addOverlay(markers[<%=anum%>]); <% Next %> } // end of the function // function centerMap( lat, lng ) { googleMap.setCenter(new GLatLng(lat,lng)); } // Called when the user approves an address: function approve(which,aNum,aStreet,aSuburb,aPCode) { var suburb_asis = "<%=Trim(suburb_asis)%>"; var pcode_asis = "<%=Trim(pcode_asis)%>"; // if the postcode given by the user was not changed by google, // then accept their word for the suburb: if ( aPCode == pcode_asis ) aSuburb = suburb_asis; var frm = opener.document.featured_ad; frm.addressValidate.value = "1"; frm.latitude.value = latitudes[which]; frm.longitude.value = longitudes[which]; // then utilize the values that Google found: if ( aNum.replace(/\s/g,"") != "" ) frm.share_number.value = aNum; if ( aStreet.replace(/\s/g,"") != "" ) frm.share_street.value = aStreet; aSuburb = aSuburb.replace(/^\+s/,"").replace(/\s+$/,"").toUpperCase(); // trim var sel = frm.share_suburb; sel.selectedIndex = 0; if ( aSuburb != "" ) { for ( var i = 1; i < sel.options.length; ++i ) { var opt = sel.options[i]; if ( opt.text.toUpperCase().indexOf(aSuburb) == 0 ) { sel.selectedIndex = i; // frm.share_postcode.value = opt.value; break; } } } // alert(aSuburb); // alert(aPCode); // and we are done here self.close( ); } </script> </head> <body onLoad="initMap()" onUnLoad="GUnload()"> <div id="GoogleMap" style="width: 390px; height: 300px"></div> <br/> <form style="inline" ID="Form1"> <table style="width: 390px; margin-left: 10px;" border=0 cellpadding=2 class="style2" ID="Table1"> <% ' so...how many address matches did we get?? If foundAddrMax = 0 Then ' just one! So user either approves or disapproves %> <tr> <td colspan=2> Google found that address at the location<br>indicated on the map.<p> If that is correct, then just click the "is Correct" button.<br> If it is not correct, then click on the "Try Again" button.<br> </td> <% Else %> <tr> <td colspan=2 style="padding-bottom: 6px;"> Google found <b><%=(foundAddrMax+1)%> location(s)</b> that match that address.<br/> Click on a numbered marker below to find it on the map.<p> If one of the numbered markers is indicating<br> your actual location, then simply click on the<br> appropriate "is Correct" button.<br> Click on "Try Again" if none are correct. </td> </tr> <% End If For btn = 0 To foundAddrMax row = addresses(btn) ' order of elements in sub array is address, lat, long, street, suburb, pcode address = row(0) lat = row(1) lng = row(2) temp = row(3) spaceAt = InStr( temp, " " ) aNum = "" aStreet = temp ' assume the worst If spaceAt > 0 Then aNum = Left(temp,spaceAt-1) If IsNumeric(aNum) Then aStreet = Mid(temp,spaceAt+1) ' best case: number and street Else aNum = "" ' nope, back to worst case End If End If aSuburb = row(4) aPCode = row(5) url = "javascript:centerMap(" & lat & "," & lng & ");" %> <tr> <td rowspan=2 valign=middle ><a href="<%=url%>"><img src="/images/marker<%=(btn+1)%>.png" border=0></a></td> <td><%=address%></td> </tr> <tr> <td> <? //SET A REPLACE FUNCTION ON ANY POSSIBLE APOSTROPHES aStreet = Replace( aStreet, "'", "" ) aSuburb = Replace( aSuburb, "'", "" ) ?> <input type=button class="sbttnorange" value="Marker <%=(btn+1)%> is Correct" onclick="approve(<%=btn%>,'<%=aNum%>','<%=aStreet%>','<%=aSuburb%>','<%=aPCode%>');"><br> </td> </tr> <? Next Else //' else if foundAddrMax < 0 .... meaning no matches on address ?> <body> <form style="inline" ID="Form2"> Sorry, Google Maps couldn't find that address or variations on it. Try again. <? End If ?> <tr> <td></td> <td style="padding-top: 10px;"> <input type=button class="sbttnorange" value="Close Window Try Again" onClick="self.close();"> </td> </tr> </table> </form> </body> </html> </body> </html> Edited May 5, 2013 by ramone_johnny Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.