我正在使用一个名为GeoPortal的 Google Maps Geometry 插件来对建筑物的屋顶进行测量。
我已经让插件在我的应用程序中正常工作,它获取上一页的经度和纬度,它们是从输入的地址动态创建的。
我遇到的问题是人们需要能够在应用程序中前后导航,这意味着需要记住区域图,因此当他们导航回该页面时,他们不必每次都重新绘制区域。
例如,当用户导航回页面时,我需要记住这个绘图布局:
我的代码如下:
jQuery
var map;
var measure = {
mvcLine: new google.maps.MVCArray(),
mvcPolygon: new google.maps.MVCArray(),
mvcMarkers: new google.maps.MVCArray(),
line: null,
polygon: null
};
jQuery(document).ready(function() {
map = new google.maps.Map(document.getElementById("map"), {
zoom: 21,
center: new google.maps.LatLng(51.502378,-0.114348),
mapTypeId: google.maps.MapTypeId.HYBRID,
draggableCursor: "crosshair"
});
google.maps.event.addListener(map, "click", function(evt) {
measureAdd(evt.latLng);
});
});
function measureAdd(latLng) {
var marker = new google.maps.Marker({
map: map,
position: latLng,
draggable: true,
raiseOnDrag: false,
title: "Drag me to change shape",
icon: new google.maps.MarkerImage("http://maps.co.mecklenburg.nc.us/geoportal/img/measure-vertex.png", new google.maps.Size(9, 9), new google.maps.Point(0, 0), new google.maps.Point(5, 5))
});
measure.mvcLine.push(latLng);
measure.mvcPolygon.push(latLng);
measure.mvcMarkers.push(marker);
var latLngIndex = measure.mvcLine.getLength() - 1;
google.maps.event.addListener(marker, "mouseover", function() {
marker.setIcon(new google.maps.MarkerImage("http://maps.co.mecklenburg.nc.us/geoportal/img/measure-vertex-hover.png", new google.maps.Size(15, 15), new google.maps.Point(0, 0), new google.maps.Point(8, 8)));
});
google.maps.event.addListener(marker, "mouseout", function() {
marker.setIcon(new google.maps.MarkerImage("http://maps.co.mecklenburg.nc.us/geoportal/img/measure-vertex.png", new google.maps.Size(9, 9), new google.maps.Point(0, 0), new google.maps.Point(5, 5)));
});
google.maps.event.addListener(marker, "drag", function(evt) {
measure.mvcLine.setAt(latLngIndex, evt.latLng);
measure.mvcPolygon.setAt(latLngIndex, evt.latLng);
});
google.maps.event.addListener(marker, "dragend", function() {
if (measure.mvcLine.getLength() > 1) {
measureCalc();
}
});
if (measure.mvcLine.getLength() > 1) {
if (!measure.line) {
measure.line = new google.maps.Polyline({
map: map,
clickable: false,
strokeColor: "#FF0000",
strokeOpacity: 1,
strokeWeight: 3,
path:measure. mvcLine
});
}
if (measure.mvcPolygon.getLength() > 2) {
if (!measure.polygon) {
measure.polygon = new google.maps.Polygon({
clickable: false,
map: map,
fillOpacity: 0.25,
strokeOpacity: 0,
paths: measure.mvcPolygon
});
}
}
}
if (measure.mvcLine.getLength() > 1) {
measureCalc();
}
}
function measureCalc() {
var length = google.maps.geometry.spherical.computeLength(measure.line.getPath());
jQuery("#span-length").text(length.toFixed(1))
if (measure.mvcPolygon.getLength() > 2) {
var area = google.maps.geometry.spherical.computeArea(measure.polygon.getPath());
jQuery("#span-area").text(area.toFixed(1));
document.getElementById('RoofArea').value = area.toFixed(1);
document.getElementById('submit').disabled = false;
}
}
function measureReset() {
if (measure.polygon) {
measure.polygon.setMap(null);
measure.polygon = null;
}
if (measure.line) {
measure.line.setMap(null);
measure.line = null
}
measure.mvcLine.clear();
measure.mvcPolygon.clear();
measure.mvcMarkers.forEach(function(elem, index) {
elem.setMap(null);
});
measure.mvcMarkers.clear();
jQuery("#span-length,#span-area").text(0);
document.getElementById('submit').disabled = true;
}
HTML/CSS
<style>
#map {
width: 500px;
height: 400px;
margin: 0 auto;
border: 5px solid #2F4B9E;
}
</style>
<div>
<div id="map"></div>
<p>Length (red line): <span id="span-length">0</span> mt<br />
Area (grey area): <span id="span-area">0</span> mt²<br />
<a href="javascript:measureReset();">Reset Measure</a></p>
</div>