GOOGLE地图简单操作

最近项目中用到google地图,简单研究了下,记录下代码,备用!

  

    var map;
    var markerArr = new Array();

 

//初始化google地图
function initialize() { var mapOptions = { center: new google.maps.LatLng(39.9, 116.40), zoom: 4, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); }

//添加标记
function AddMarker(deviceName,lat,lng) { var image = { url: 'image/carOver0.bmp' }; var myLatLng = new google.maps.LatLng(lat,lng); var beachMarker = new google.maps.Marker({ position: myLatLng, map: map, icon: image, title:deviceName }); markerArr.push(beachMarker); //弹窗 var infowindow = new google.maps.InfoWindow({ content: 'hello world!' });
    //为marker添加点击事件 google.maps.event.addListener(beachMarker,
'click', function() { infowindow.open(map,beachMarker); }); }
   
//删除标记
function RemoveMarker(deviceName) { var index; for (var i = 0; i < markerArr.length; i++) { var m = markerArr[i];//这个地方如果直接用markerArr[i].setMap(NULL)会把所有的标记都从地图删除,不懂,求指教! if (m.title == deviceName) { m.setMap(null); index = i; }; }; markerArr.splice(index,1); }
    
//更新标记位置
function UpdateCarLocation(deviceName,lat,lng) { var location = new google.maps.LatLng(lat,lng); for (var i = 0; i < markerArr.length; i++) { var marker = markerArr[i]; if (marker.title == deviceName) { marker.setPosition(location); }; }; }

 

posted @ 2013-12-04 11:07  cg.2019  阅读(268)  评论(0编辑  收藏  举报