我目前正在为大学制作一个项目,并且我试图在我的网站上实施谷歌地图
ReportStolenBike.js:
var mapCenter = new google.maps.LatLng(51.8979988098144,-2.0838599205017);
var geocoder = new google.maps.Geocoder();
var infowindow = new google.maps.InfoWindow();
var myMap;
var marker;
function initialize(){
var mapOptions = {
zoom: 15,
center: mapCenter
};
myMap = new google.maps.Map(document.getElementById("mapInput"), mapOptions);
marker = new google.maps.Marker({
map: myMap,
position: mapCenter,
draggable: true
});
function markerDragged() {
var selectedPos = {'latLng': marker.getPosition()};
geocoder.geocode(selectedPos, showAddressInInfoWindow);
}
google.maps.event.addListener(marker, 'dragend', markerDragged);
function showAddressInInfoWindow(results) {
if (results[0]) {
infowindow.setContent(results[0].formatted_address);
infowindow.open(myMap, marker);
}
}
}
google.maps.event.addDomListener(window, 'load', initialize);
$('#formReportStolen').on('submit', function(e) {
var formData = new FormData(this);
var lat = marker.getPosition().lat();
var lng = marker.getPosition().lng();
formData.append("lat", lat);
formData.append("lng", lng);
e.preventDefault();
$.ajax({
url: "PublicReportStolenDAO",
method : "POST",
data: formData,
contentType: false,
cache: false,
processData: false,
success:function(echoedMsg) {
if (echoedMsg == "True") {
alert("Successfully reported.");
}
}
});
});
因此,在那个java脚本类中,我显示了我的谷歌地图,并尝试将用户当前的位置。我的地图有一个弹出窗口,显示我的地图确实引用了纬度和经度,但是当我试图将它们存储到我的sql数据库时,没有任何东西存储。
PublicReportStolenDAO.php
<?php
function StolenReg(){
$bikeID = $_POST['txtBikeID'];
$date = $_POST['txtDateofTheft'];
$lat = $_POST['lat'];
$lng = $_POST['lng'];
include "../../include/config.php";
$verifyFuncBike = "SELECT email FROM `tbl_user` WHERE email = '$email'";
$resultBike = mysqli_query($connection,$verifyFuncBike);
$rowBike = mysqli_fetch_array($resultBike,MYSQLI_ASSOC);
$countBike = mysqli_num_rows($resultBike);
if($countBike == 1) {
$sql = "INSERT INTO `tbl_stolen`(BikeID, Date, Lat, Lng)".
" VALUES ".
"('$bikeID', '$date', '$lat', '$lng')";
if(mysqli_query($connection, $sql)) {
echo "Report successfully sent, please return back to the site.";
echo $lat;
echo $lng;
} else {
echo mysqli_error($connection);
}
}else {
echo "Error, please return back to the site and try again.";
}
mysqli_close($connection);
}
stolenReg();
?>
对于可能很糟糕的代码感到抱歉,我对Web开发非常陌生
答案 0 :(得分:0)
我不认为这是最好的答案。但在你的代码中使用
var lat = marker.getPosition().lat();
这不是获得坐标的功能。获得你需要地理定位的坐标。所以它应该是
var lat = position.coords.latitude
var lat = position.coords.longitude
为了确保您的位置,您可以使用此
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var lat = position.coords.latitude
var lat = position.coords.longitude
$.ajax({
do your ajax
});
});
}
抱歉我的不好意思。希望这对你有帮助。