gpt4 book ai didi

javascript - Google Maps API v3 从 drawingManager 保存和重用形状

转载 作者:行者123 更新时间:2023-11-30 09:58:37 24 4
gpt4 key购买 nike

我正在使用 google 提供的 google map api 示例之一。在这个例子中,我们可以使用绘图库在 map 上绘制一些线。

假设我画了一些东西。那我怎么分享这张图呢?还是保存以供日后引用?

下面是代码

function initMap() {

var map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: -34.397,
lng: 150.644
},
zoom: 11,
// only show roadmap type of map, and disable ability to switch to other type
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false
});

var drawingManager = new google.maps.drawing.DrawingManager({
drawingControl: true,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: [
google.maps.drawing.OverlayType.POLYGON, ],
clickable: true,
draggable: true
},
polygonOtions: {
clickable: true,
draggable: true
}
});

drawingManager.setMap(map);
}

更新:

我正在尝试 Vadim 的解决方案,但似乎存在错误。画点东西然后刷新你会看到

这是产生错误的代码:

<!DOCTYPE html>
<html>
<head>

<style>
html, body {
margin: 0;
padding: 0;
height:100%;
}
#map {
height: 100%;
}
.btn {
position:absolute;
width:50px;
height:60px;
top:5%;
left: 50%;
z-index:9999;
color:black;
}
</style>

<script src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=drawing,geometry,places"></script>
</head>
<body>
<div class="btn" onclick="clearall(map);">delete</div>
<div id="map"></div>
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: { lat: -34.397, lng: 150.644 },
zoom: 4,
// only show roadmap type of map, and disable ability to switch to other type
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false
});

map.data.setControls(['Polygon']);
map.data.setStyle({
editable: true,
draggable: true
});

map.data.addListener('addfeature', savePolygon);
map.data.addListener('removefeature', savePolygon);
map.data.addListener('setgeometry', savePolygon);

//load saved data
loadPolygons(map);
}

function loadPolygons(map) {
var data = JSON.parse(sessionStorage.getItem('geoData'));
// map.data.forEach(function (f) {
// map.data.remove(f);
// });
map.data.addGeoJson(data)
}

function savePolygon() {
map.data.toGeoJson(function (json) {
// console.log(JSON.stringify(json));
sessionStorage.setItem('geoData', JSON.stringify(json));
});
}
function clearall(map){
map.data.forEach(function (f) {
map.data.remove(f);
});
}

initMap();

</script>

</body>
</html>

最佳答案

您可以利用 Google Maps Data layer为了这个目的。下面的示例演示了如何使用 google.maps.Data 类将多边形导出和导入为 GeoJSON 数据。 localStorage用于存储 GeoJSON 数据。

var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: { lat: -34.397, lng: 150.644 },
zoom: 4,
// only show roadmap type of map, and disable ability to switch to other type
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false
});

map.data.setControls(['Polygon']);
map.data.setStyle({
editable: true,
draggable: true
});
bindDataLayerListeners(map.data);

//load saved data
loadPolygons(map);
}


// Apply listeners to refresh the GeoJson display on a given data layer.
function bindDataLayerListeners(dataLayer) {
dataLayer.addListener('addfeature', savePolygon);
dataLayer.addListener('removefeature', savePolygon);
dataLayer.addListener('setgeometry', savePolygon);
}

function loadPolygons(map) {
var data = JSON.parse(localStorage.getItem('geoData'));
map.data.forEach(function (f) {
map.data.remove(f);
});
map.data.addGeoJson(data)
}



function savePolygon() {
map.data.toGeoJson(function (json) {
localStorage.setItem('geoData', JSON.stringify(json));
});
}

Demo

更新

以下demo演示了如何删除多边形。

关于javascript - Google Maps API v3 从 drawingManager 保存和重用形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32739660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com