我需要在谷歌地图上合并两种类型的绘图。
1:多边形- https://developers.google.com/maps/documentation/javascript/examples/polygon-arrays
2: Marker - https://developers.google.com/maps/documentation/javascript/examples/infowindow-simple
我试图合并代码,并做了各种调整,但它不工作。它要么显示其中一个,要么显示另一个,要么显示空白页。
它甚至可能合并两个图纸在一个单一的地图?有人能举个例子吗?
多边形和标记在同一张地图上的简单例子:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Polygon+marker example</title>
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: {lat: 24.886, lng: -70.268},
mapTypeId: 'terrain'
});
// Define the LatLng coordinates for the polygon.
var triangleCoords = [
{lat: 25.774, lng: -80.190},
{lat: 18.466, lng: -66.118},
{lat: 32.321, lng: -64.757}
];
// Construct the polygon.
var bermudaTriangle = new google.maps.Polygon({
paths: triangleCoords,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 3,
fillColor: '#FF0000',
fillOpacity: 0.35
});
bermudaTriangle.setMap(map);
// marker
var uluru = {lat: 21.5012681, lng: -84.0397756};
var marker = new google.maps.Marker({
position: uluru,
map: map,
title: 'marker title'
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
</script>
</body>
</html>
不要忘记添加你的google maps API key而不是YOUR_API_KEY