TimStone wrote:The translation time for the geocode is quite long when creating a map. So I decided to get the codes and store them in the database for each address.
Obviously this will take time because google has to search its database based on the address provided by you and then return the most accurate result from a bunch of data. If your list is too long then it increases the time further. For this reason, I keep the Lat and Lng in the database. May be you can run this routine once in a while to update the Lat and Lng in your Customer address DBF/Table. For large list of address there are services from Google named Google App Scripts.
My initial example in this thread was to display a Google Map based on the available Lat and Lng. I created the sample to get the Lat and Lng based on the address when you said that your requirement was to display a Googe Map based on the Address details that you have in your database. It is just a sample to demonstrate that this is possible using FiveWin. Its my mistake that, I have not gone deep into catching the run time errors if there are no results from Google.
TimStone wrote:Thoughts on trapping hjson values that do not have an array would be appreciated.
You should add the lat long to the array only if hJson["status"] == "OK"
OR you can put a try catch
Anyway here is one of the methods to consider to gracefully control the run time error which may occur if the searched address is not found by Google.
#Include "FiveWin.ch"
//-----------------------------
Function Main()
聽 聽 Local aLatLng:={}
聽 聽 Local aAddress:={ {"Anser","K K" 聽 聽 聽 聽 聽 聽, "just dummy so that it 聽", "will not give" 聽 聽 聽 ,"any results" },;
聽 聽 聽 聽 聽 聽聽 聽 聽 聽 聽 聽{"Eiffel Tower","Champ de Mars" 聽 聽 聽 聽 聽 聽, "5 Avenue Anatole France", "75007 Paris" 聽 聽 聽 聽 ,"France" },;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 {"Taj Mahal" 聽 ,"Dharmapuri, Forest Colony", "Tajganj, Agra" 聽 聽 聽 聽 聽, "Uttar Pradesh 282001","India" 聽 } }
聽 聽 // This function will grab the Lat and Lng information from Google and returns an array
聽 聽 aLatLng:=GetLatLng(aAddress)
聽 聽 // Displays Markers on a Google Map
聽 聽 ViewGoogleMap(aLatLng)
Return NIL
//------------------------------------------//
Function GetLatLng(aData)
聽 Local i,cName,cAddress,cCity,cState,cCountry,aLatLng:={},nLatitude,nLongitude
聽 Local oHttp, cURL, lNetError, cResponse,hJson
聽
聽 For i:=1 to Len(aData)
聽
聽 聽 cName:=STRTRAN(ALLTRIM(aData[i][1])," ","+")
聽 聽 cName:=STRTRAN(ALLTRIM(aData[i][1]),"&"," E ")
聽 聽 cAddress:=STRTRAN(ALLTRIM(aData[i][2]),",","")
聽 聽 cAddress:=STRTRAN(ALLTRIM(aData[i][2])," ","+")
聽 聽 cCity:=STRTRAN(ALLTRIM(aData[i][3])," ","+")
聽 聽 cCountry:=STRTRAN(ALLTRIM(aData[i][5])," ","+") 聽
聽 聽 cState:=aData[i][4]
聽 聽
聽 聽 oHttp:=CreateObject("Microsoft.XMLHTTP")
聽 聽 cURL:="http://maps.google.com/maps/api/geocode/json?address="+cAddress+"+"+cCity+"+-+"+cState+"+"+cCountry+"&sensor=false"
聽 聽 oHttp:Open("GET",cURL,.F.)
聽 聽 lNetError:=.F.
聽 聽 TRY
聽 聽 聽 聽 oHttp:Send()
聽 聽 CATCH oError
聽 聽 聽 聽 lNetError:=.T.
聽 聽 END TRY
聽 聽
聽 聽 IF !lNetError
聽 聽 聽 cResponse := oHttp:ResponseBody
聽 聽 ELSE
聽 聽 聽 // Search Error. Could not find the details on Google Maps.
聽 聽 聽 Loop
聽 聽 ENDIF 聽 聽
聽 聽 hb_jsonDecode(cResponse,@hJson)
聽 聽 IF hJson["status"] == "OK"
聽 聽 聽 聽 nLatitude:= hJson["results"][1]["geometry"]["location"]["lat"]
聽 聽 聽 聽 nLongitude:=hJson["results"][1]["geometry"]["location"]["lng"] 聽 聽
聽 聽 聽 聽 Aadd(aLatLng,{aData[i][1],nLatitude, nLongitude} )
聽 聽 ENDIF
聽 聽
聽 Next
聽
Return aLatLng
//-----------------------------------------------------------------------/
Function ViewGoogleMap(aData)
聽 聽Local cMapFile:="D:\GMaps.htm", cHtmlContent1,cHtmlContent2, oOle, i
聽 聽Local cAppendStr:="var locations = ["+CRLF
聽 聽
聽 聽If Len(aData) == 0
聽 聽 聽 MsgInfo("Location data not available in the list")
聽 聽 聽 Return
聽 聽Endif
聽 聽
聽 聽For i:=1 to Len(aData)
聽 聽 聽 聽cAppendStr+=Space(4)+"['" +aData[i][1] +"',"+Ltrim(Str(aData[i][2]))+","+ Ltrim(Str(aData[i][3]))+ If( i < Len(aData), "],", "]") +CRLF
聽 聽Next
聽 聽cAppendStr+="];"+CRLF
TEXT INTO cHtmlContent1
聽 聽
聽 聽 <html>
聽 聽 <head>
聽 聽 聽
聽 聽 聽 <title>Google Maps Multiple Markers</title>
聽 聽 聽 <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
聽 聽 </head>
聽 聽 <body>
聽 聽 聽 <div id="map" style="height: 100%; width: 100%;">
聽 聽 </div>
聽 聽 <script type="text/javascript">
聽 聽
ENDTEXT
聽 聽
聽 聽
TEXT INTO cHtmlContent2 聽 聽
聽 聽 聽 聽
聽 聽 聽 聽 var map = new google.maps.Map(document.getElementById('map'), {
聽 聽 聽 聽 聽 zoom: 0, 聽 聽 聽 聽 聽
聽 聽 聽 聽 聽 mapTypeId: google.maps.MapTypeId.ROADMAP
聽 聽 聽 聽 });
聽 聽 聽 聽 var bounds = new google.maps.LatLngBounds();
聽 聽 聽 聽 var infowindow = new google.maps.InfoWindow();
聽 聽
聽 聽 聽 聽 var marker, i;
聽 聽
聽 聽 聽 聽 for (i = 0; i < locations.length; i++) {
聽 聽 聽 聽 聽var position = new google.maps.LatLng(locations[i][1], locations[i][2]);
聽 聽 聽 聽 聽 聽 bounds.extend(position);
聽 聽 聽 聽 聽 marker = new google.maps.Marker({
聽 聽 聽 聽 聽 聽 position: new google.maps.LatLng(locations[i][1], locations[i][2]), 聽 聽 聽 聽
聽 聽 聽 聽 聽 聽 map: map
聽 聽 聽 聽 聽 });
聽 聽
聽 聽 聽 聽 聽 google.maps.event.addListener(marker, 'click', (function(marker, i) {
聽 聽 聽 聽 聽 聽 return function() {
聽 聽 聽 聽 聽 聽 聽 infowindow.setContent(locations[i][0]);
聽 聽 聽 聽 聽 聽 聽 infowindow.open(map, marker);
聽 聽 聽 聽 聽 聽 }
聽 聽 聽 聽 聽 })(marker, i));
聽 聽 聽 聽 聽 // Automatically center the map fitting all markers on the screen
聽 聽 聽 聽 聽 map.fitBounds(bounds);
聽 聽 聽 聽 聽
聽 聽 聽 聽 }
聽 聽 聽 </script>
聽 聽 </body>
聽 聽 </html>
聽 聽
ENDTEXT
聽 聽 聽MEMOWRIT( cMapFile, cHtmlContent1+cAppendStr+cHtmlContent2 )
聽 聽 聽Shellexecute( NIL, "open", cMapFile ) 聽 聽
聽 聽
Return

