在谷歌地图上绘制多个点
问题描述:
您好,
i是在VS 2010中编写代码的新手。我必须创建一个Windows窗体应用程序,我需要从ms访问文件中检索坐标并绘制所有检索到的点在谷歌地图上。
请告诉我该怎么做?
提前谢谢:)
Hello,
i am new to writing a code in VS 2010. I have to create a windows form application in which I need to retrieve coordinates from an ms-access file and plot all the retrieved points on google map.
please tell How should i do this?
Thanks in advance :)
答
您可以使用gmap v3版本。只需从您的数据库中获取地址,使用gmap的地理编码器并将该地址传递给它。这将为您提供坐标,即纬度和经度,现在将其绘制在地图上。
请参考以下代码:
Hi,
You can use gmap v3 version. Just take the address from your database use geocoder of gmap and pass that address to it. This will give you the co-ordinates i.e. latitude and longitude now plot it on the map.
Please refer from below code:
function getBuildingsCoordinates(buildingAddressAll)
{
var j=0;//Counter for call back.
var geocoder=new google.maps.Geocoder();//Creating Object.
var tempArray = new Array();//Array holds the all building details with special characters temporary.
//This block is for multiple building
if(buildingAddressAll!=null)
{
tempArray = buildingAddressAll.split('~');//Getting all building details in array.
for (var i = 0; i < tempArray.length; i++)//Loop for getting coordinates one by one of every building.
{
var ArrAddress=new Array();//Holds all building addresses.
var tempVar=tempArray[i];
buildingInfo=tempVar.split('###');//Object that holds the info of single building.
buildingInfoAll[i]=buildingInfo;/*Created a nested array.Each nested array contains information[kioskID,buildingID,
buildingType,buildingAddress,buildingName,buildingLatLng]of individual building.*/
ArrAddress[i] = buildingInfo[3];
var myaddress=ArrAddress[i];//This attribute hold the address of building from array of multiple building address.
posn = myaddress.indexOf("%20");//Removing space from address
while (posn > -1)//while there is a %20
{
myaddress = myaddress.substring(0,posn) + " " + myaddress.substring(posn+3);
posn = myaddress.indexOf("%20");//find next %20
};
if(!myaddress)
{
alert('Address not found for building: '+buildingInfoAll[i][4]);
}
else
{
geocoder.geocode( { 'address': myaddress}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK)
{
buildingInfoAll[j].push(results[0].geometry.location);//Collecting address co-ordinates.
j++;
if(ArrAddress.length == j)
{
initialize(buildingInfoAll);//Initializing map when all co-ordinates for buildings are collected.
}
}
else
{
alert('Geocode was not successful for Building '+buildingInfoAll[i][4]+' due to: ' + status);
}
});
}
}
}
在上面的代码中,我找到了所有纬度和经度一旦所有坐标调用了一个回调函数调用inInize方法,通过传递这个数组即buildingInfoAll。
In the above code I am finding all the latitude and longitude and holding in buildingInfoAll array once all the co-ordinates you got a call back function called which calls intialize method by passing this array i.e. buildingInfoAll.
function initialize(buildingInfoAll) {
// Now extract the co-ordinates from the buildingInfoAll array and pass it to myLatLng. Below its given hard coded.
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var mapOptions = {
zoom: 4,
center: myLatlng
}
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: 'Hello World!'
});
}
google.maps.event.addDomListener(window, 'load', initialize);