我目前正在编写一个代码,当用户输入位置时,他将获得经度n纬度,但我需要将lat和long添加到数据库。客户端代码正在运行html文件,如下所示。
如何在php中为服务器端传输这些值并将它们添加到数据库中。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="UTF-8">
<title>maps</title>
<style>
#map_canvas { width:400px; height:450px; }
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js? sensor=false">
</script>
<script type="text/javascript">
var geocoder;
var map;
var lat;
var lng;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-30.070, -51.190);
var myOptions = {
zoom: 9,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}
function codeAddress() {
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker;
document.getElementById('lat').value = results[0].geometry.location.lat();
document.getElementById('lng').value = results[0].geometry.location.lng();
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
</head>
<body onload="initialize()">
<form action="map2.php" method="get">
<div id="map_canvas"></div>
<div>
<input id="address" type="textbox" value="">
<input type="button" value="Localizar!" onclick="codeAddress()"><br>
Latitude: <input type="text" id="lat"><br>
Longitude: <input type="text" id="lng"><br>
</div>
答案 0 :(得分:5)
您可以使用jquery轻松完成此操作(无需加载页面或提交表单) -
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker;
document.getElementById('lat').value = results[0].geometry.location.lat();
document.getElementById('lng').value = results[0].geometry.location.lng();
// add this
var latt=results[0].geometry.location.lat();
var lngg=results[0].geometry.location.lng();
$.ajax({
url: "your-php-code-url-to-save-in-database",
dataType: 'json',
type: 'POST',
data:{ lat: lat, lng: lngg }
success: function(data)
{
//check here whether inserted or not
}
});
}
并在php文件中:
<?php
// file name: your-php-code-url-to-save-in-database.php
$lat=$_POST['lat'];
$lng=$_POST['lng'];
// write your insert query here to save $lat and $lng
?>
了解更多内容并下载Jquery访问:http://jquery.com/download/
另一种方法是没有Jquery(Page将重定向到map2.php):
<div id="map_canvas"></div>
<form action="map2.php" method="get">
<div>
<input id="address" type="textbox" value="">
<input type="button" value="Localizar!" onclick="codeAddress()"><br>
Latitude: <input name="lat" type="text" id="lat"><br>
Longitude: <input name="lng" type="text" id="lng"><br>
</div>
</form>
并在你的map2.php中写 -
<?php
// file name: map2.php
$lat=$_GET['lat'];
$lng=$_GET['lng'];
// write your insert query here to save $lat and $lng
//get your mysql db connection ready
$sql="insert into table_name (latitude, longitude) values('".$lat."','".$lng.")";
$command=mysql_query($sql);
// close databse connection and everythig is done then redict to any page.
//check your database table to see the inserted value
?>
在您的表单方法中,我建议使用 POST 而不是 GET 。如果您进行了此更改,那么在map2.php中只需更改 GET 强>仅<到发布。要了解使用POST的原因,请访问Link 1和Link 2