我有一个谷歌地图,用户可以在其中创建折线。我已将折线(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);
});
});
};
})();
答案 0 :(得分:2)
您可以随时存储它 - 这取决于您以后如何使用这些数据。 您可以将它作为JSON编码数组存储在TEXT字段中,您可以将lat / lng坐标保存在单独的字段中,如下所示:
// Table for polylines
CREATE TABLE `polylines` (
`pID` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`pName` VARCHAR(45) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`pID`)
)
ENGINE = MyISAM
CHARACTER SET utf8 COLLATE utf8_general_ci;
// Table for coordinates
CREATE TABLE `coords` (
`cID` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`cPolyline` INTEGER UNSIGNED NOT NULL,
`cLat` VARCHAR(15) NOT NULL,
`cLng` VARCHAR(15) NOT NULL,
PRIMARY KEY (`cID`)
)
ENGINE = MyISAM
CHARACTER SET utf8 COLLATE utf8_general_ci;
现在你可以得到这样的折线:
SELECT * FROM polylines WHERE pName = 'something'
然后坐标:
SELECT cLat, cLng FROM coords WHERE cPolyline = ID
有很多可能性和选择 - 就像我之前说的那样 - 这一切都取决于你接下来要做什么。
答案 1 :(得分:2)
现在,这是适合您的数据库解决方案:
表路径将存储您的阵列中的路径。
CREATE TABLE `gmap`.`paths` (
`pID` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`pName` VARCHAR(75) NOT NULL,
`pStartLat` VARCHAR(25) NOT NULL,
`pStartLng` VARCHAR(25) NOT NULL,
`pAverageSpeed` FLOAT NOT NULL,
PRIMARY KEY (`pID`)
)
ENGINE = MyISAM;
表路径将在pName字段中存储您的用户/路径名称(无论您想要什么),pStartLat / pStartLng字段中的起点,pAverageSpeed当然是平均速度(不知道您是否需要它,以防万一)和pID是您将与另一个表一起使用的标识符:
CREATE TABLE `gmap`.`coords` (
`cID` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`cLat` VARCHAR(25) NOT NULL,
`cLng` VARCHAR(25) NOT NULL,
`cSpeed` FLOAT NOT NULL,
`cPath` INTEGER UNSIGNED NOT NULL,
PRIMARY KEY (`cID`)
)
ENGINE = MyISAM;
此表格可让您存储坐标 - 每个坐标都有速度。
现在,假设您要显示名为“TestOne”的路径。
// Connect to the database - I assume you can do that
// and retrieve data
SELECT * FROM paths WHERE pName = "TestOne"
现在你在表格中找到了ID,名称,起点坐标和平均速度(mysql_fetch_assoc对此非常有用)。
然后,使用ID可以检索其余坐标:
SELECT * FROM coords WHERE cPath = ID
现在,使用e。 G。 while循环,您可以将所有坐标检索到数组中。
当然首先您必须使用INSERT INTO构造或类似方法存储该数据: - )
答案 2 :(得分:1)
你可以使用很多解决方案,例如你的代码似乎是这样的:
var my_map= new GMap2(document.getElementById("my_map"));
my_map.setCenter(new GLatLng(36.82,10.17),10);
my_map.addControl(new GSmallMapControl());
GEvent.addListener(my_map, "click", function(overlay,point){
var lat = point.lat();
var lng = point.lng();
$('#input_lat').val(lat);
$('#input_long').val(lng);
});
和input_lat,input_long可以隐藏或可见输入到您的表单中,然后保存到您的数据库中