#Include "FiveWin.ch"
//-----------------------------
Function Main()
Local aLatLng:={}
Local aAddress:={ {"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)
nLatitude:= hJson["results"][1]["geometry"]["location"]["lat"]
nLongitude:=hJson["results"][1]["geometry"]["location"]["lng"]
Aadd(aLatLng,{aData[i][1],nLatitude, nLongitude} )
Next
Return aLatLng
//-----------------------------------------------------------------------/
Function ViewGoogleMap(aData)
Local cMapFile:="D:\GMaps.htm", cHtmlContent1,cHtmlContent2, oOle, i
Local cAppendStr:="var locations = ["+CRLF
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 )
/*
oOle:=CreateObject("InternetExplorer.Application")
oOle:width:=675
oOle:height:=520
oOle:Visible:=.t. // Displays the Browser
oOle:ToolBar:=.f. // Disables the toolbar
oOle:StatusBar:=.f. // Disables status bar
oOle:MenuBar:=.f. // Disables the menu bar
oOle:Navigate(cMapFile) // Open the Webpage
SysRefresh()
*/
// Opens the Map using your default internet browser. A more generic solution
Shellexecute( NIL, "open", cMapFile )
Return