将Google地图的坐标传输到mysql数据库 [英] Transfer google map coordinates to mysql database
问题描述
我有一个谷歌地图,用户可以在其中创建折线。我将折线(lat,lng)坐标存储到数组中。现在我想用php或javascript将这些坐标转移到mysql数据库,还是有其他更好的选择?我应该如何进行?
以下是我的代码:
(函数(){
window.onload = function(){
var path;
var marker;
var infowindow;
var places = [] ;
//在HTML文件中创建对div标签的引用
var mapDiv = document.getElementById('map');
// map的选项属性
var options = {
center:new google.maps.LatLng(-20.2796,57.5074),
zoom:16,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
//创建地图对象
var map = new google.maps.Map(mapDiv,options);
//创建要填充的MVC数组点击事件
var route = new google.maps.MVCArray();
var polyline = new google.maps.Polyline({
path:route,
strokeColor:'#ff0000',
strokeOpacity:0.6,
strokeWeight:5
});
polyline.setMap(地图);
//创建点击事件,附加到地图并填充路线数组
google.maps.event.addListener(map,'click',function(e){
//创建对多段线对象的引用
path = polyline.getPath();
//将在地图上单击的位置添加到MVC数组
路径。推(e.latLng);
});
//显示坐标
document.getElementById('coords')。onclick = function(){
for(var i = 0; i< path .getLength(); i ++){
alert('coords:'+ path.getAt(i).toUrlValue(2)+'\\\
');
}
}
//点击右键创建标记
google.maps.event.addListener(地图,'rightclick' ,功能(e){
marker = new google.maps.Marker({
position:e.latLng,
map:map
});
places.push(e.latLng);
//点击
$ b $获取infowindow中的坐标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);
});
} );
};
})();
现在,这里是您的数据库解决方案:
表格路径将存储您的数组中的路径。
`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是您将用于另一个表的标识符:
< pre codeREATE 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'的路径。
//连接到数据库 - 我假设你可以做
//并检索数据
SELECT * FROM paths WHERE pName =TestOne
现在你在表中获得了ID,名字,起始点坐标和平均速度(mysql_fetch_assoc对此很有帮助)。
然后,使用ID可以检索其余坐标:
SELECT * FROM coords WHERE cPath = ID
现在,使用e。 G。 while循环,你可以检索所有坐标到数组中。
当然,首先你必须使用INSERT INTO构造或类似的方式来存储这些数据: - )
I have a google map where the user can create a polyline. I have stored the polyline (lat,lng)coordinates into an array. Now i want to use php or javascript to transfer these coordinates to a mysql database or is there other better options? How should i proceed?
Here is my code:
(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);
});
});
};
})();
All right now, here is the database solution for you:
Table paths will store the paths you have, from your array.
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;
Table paths will store your user/path name (whatever you want) in pName field, starting point in pStartLat/pStartLng fields, pAverageSpeed is of course average speed (don't know if you want it, just in case) and pID is identifier which you will use with another table:
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;
This table will let you store coordinates - with speed to each one.
Now, let's say you want to show path called 'TestOne'.
// Connect to the database - I assume you can do that
// and retrieve data
SELECT * FROM paths WHERE pName = "TestOne"
Now you got ID, name, starting point coordinates and average speed in table (mysql_fetch_assoc would be great for that).
Then, using the ID you can retrieve the rest of the coordinates:
SELECT * FROM coords WHERE cPath = ID
And now, using e. g. while loop, you can retrieve all coordinates into an array.
Of course first you have to store that data using INSERT INTO construction or similar :-)
这篇关于将Google地图的坐标传输到mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!