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.
- Code: Select all Expand view RUN
- #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