我有一个谷歌地图,用户可以在其中创建折线。我已将折线 (lat,lng) 坐标存储到一个数组中。现在我想使用 php 或 javascript 将这些坐标传输到 mysql 数据库,或者还有其他更好的选择吗?我应该如何进行?
这是我的代码:
(function() {
window.onload = function() {
var path;
var marker;
var infowindow;
var places = [];
//create reference to div tag in HTML file
var mapDiv = document.getElementById('map');
// option properties of map
var options = {
center: new google.maps.LatLng(-20.2796, 57.5074),
zoom : 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// create map object
var map = new google.maps.Map(mapDiv, options);
// create MVC array to populate on click event
var route = new google.maps.MVCArray();
var polyline = new google.maps.Polyline({
path: route,
strokeColor: '#ff0000',
strokeOpacity: 0.6,
strokeWeight: 5
});
polyline.setMap(map);
// create click event,attach to map and populate route array
google.maps.event.addListener(map,'click', function(e) {
// create reference to polyline object
path = polyline.getPath();
// add the position clicked on map to MVC array
path.push(e.latLng);
});
// display coordinates
document.getElementById('coords').onclick = function() {
for(var i = 0; i < path.getLength(); i++) {
alert('coords: ' + path.getAt(i).toUrlValue(2) +'\n');
}
}
// create marker on right click
google.maps.event.addListener(map,'rightclick', function(e) {
marker = new google.maps.Marker({
position: e.latLng,
map: map
});
places.push(e.latLng);
// get coordinates in infowindow on click
google.maps.event.addListener(marker,'click', function(event){
if(!infowindow) {
infowindow = new google.maps.InfoWindow();
}
infowindow.setContent('coords: ' + marker.getPosition().toUrlValue(3));
infowindow.open(map,marker);
});
});
};
})();