在Google Map中顯示多個Marker並畫出Polygon,markerpolygon

來源:互聯網
上載者:User

在Google Map中顯示多個Marker並畫出Polygon,markerpolygon

有時需要在地圖顯示多個位置並畫出多段線


實現後:



HTML:


<!DOCTYPE html><html> <head>   <meta http-equiv="content-type" content="text/html; charset=UTF-8" />   <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="width: 500px; height: 400px;"></div>  <script type="text/javascript">    var PathData = [["hello1",49.2761419673641, -123.118069007778], ["hello2",49.2791259862655, -123.129144031353],["hello3",49.2704849721733, -123.125236002048],["hello4",49.2732990317854, -123.117229946411],["hello5",49.2761419673641, -123.118069007778]]; function initialize() {// - set map    var map =         new google.maps.Map(document.getElementById('map'));// - set info window -- will be used in marker click event    var infowindow = new google.maps.InfoWindow();// - set middle path data to center    var center =         new google.maps.LatLng(PathData[2][1], PathData[2][2]);// - set markers    var marker, i;    for (i = 0; i < PathData.length; i++) {        marker = new google.maps.Marker({        position: new google.maps.LatLng(PathData[i][1], PathData[i][2]),        map: map      });      google.maps.event.addListener(marker, 'click', (function(marker, i) {        return function() {          infowindow.setContent(PathData[i][0]);          infowindow.open(map, marker);        }      })(marker, i));    }// - set polygon     var path = [];    var bounds = new google.maps.LatLngBounds();    bounds.extend(center);    for (var i in PathData)    {        var p = PathData[i];        var latlng = new google.maps.LatLng(p[1], p[2]);        path.push(latlng);        bounds.extend(latlng);    }    var poly = new google.maps.Polygon({        paths: path,        strokeColor: '#FF0000',        strokeOpacity: 0.8,        strokeWeight: 3,        fillColor: '#FF0000',        fillOpacity: 0.1    });    poly.setMap(map);              map.fitBounds(bounds);} google.maps.event.addDomListener(window, 'load', initialize);  </script></body></html>



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.