Google 地图:如何在单个 google 地图中包含多边形和标记
GoogleMaps: How to have a polygon and a marker in a single google maps
我需要在 Google 地图上组合 2 种绘图。
1: Polygon - https://developers.google.com/maps/documentation/javascript/examples/polygon-arrays
2: Marker - https://developers.google.com/maps/documentation/javascript/examples/infowindow-simple
我尝试合并代码并进行了各种调整,但它不起作用。它显示一个或另一个或空白页。
甚至可以在一张地图上合并 2 张图纸吗?有人可以举一个有效的例子吗?
多边形和标记在同一张地图上的简单示例:
<!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 映射 API 键而不是 YOUR_API_KEY
我需要在 Google 地图上组合 2 种绘图。
1: Polygon - https://developers.google.com/maps/documentation/javascript/examples/polygon-arrays
2: Marker - https://developers.google.com/maps/documentation/javascript/examples/infowindow-simple
我尝试合并代码并进行了各种调整,但它不起作用。它显示一个或另一个或空白页。
甚至可以在一张地图上合并 2 张图纸吗?有人可以举一个有效的例子吗?
多边形和标记在同一张地图上的简单示例:
<!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 映射 API 键而不是 YOUR_API_KEY