[英]How to Update Map when Map show based on longitude and latitude i.e; fetch from database
[英]Fetch Latitude and Longitude from MySQL for show Google Map
我是google map的新手,我已將緯度和經度存儲在MySQL數據庫中。 我想顯示谷歌地圖的經度和緯度。 這是我的ajax代碼,用於從數據庫中獲取經度和緯度
$.ajax({
url: 'phpmobile/getlanglong.php',
data: { "id": getacara},
dataType: 'json',
success: function(data, status){
$.each(data, function(i,item){
showMap(item.latitude,item.longitude);
});
},
error: function(){
output.text('There was an error loading the data.');
}
});
這是我的功能showMap
function showMap(_lat,_long)
{
var map;
var myLatLng = {lat: _lat, lng: _long};
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: _lat, lng: _long},
zoom: 20
});
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Hello World!'
});
}
}
並准備好文件
$( document ).ready(function() {
showMap();
});
這是我的getlanglong.php
<?php
session_start();
include "config.php";
$idacara=mysql_real_escape_string($_GET["id"]);
$mysql = ("SELECT latitude,longitude FROM `acara` WHERE id_acara='$idacara'");
$result=mysql_query($mysql);
if (!empty($result))
{
while ($row=mysql_fetch_array($result))
{
$latlong[] = array(
'latitude' => $row['latitude'],
'longitude' => $row['longitude'],
);
}
}
mysql_close($con);
header('Content-Type:application/json');
echo json_encode($latlong);
?>
謝謝
作為加載地圖然后基於ajax請求的響應添加標記的示例(未測試)
<!doctype html>
<html>
<head>
<meta charset='utf-8'/>
<title>Google Maps</title>
<script src='https://maps.google.com/maps/api/js' type='text/javascript'></script>
<script type='text/javascript'>
(function(){
var map,marker,latlng;
/* initial locations for map */
var _lat=56;
var _lng=-2;
function showMap(){
/* set the default initial location */
latlng={ lat: _lat, lng: _lng };
/* invoke the map */
map = new google.maps.Map( document.getElementById('map'), {
center:latlng,
zoom: 20
});
/* show the initial marker */
marker = new google.maps.Marker({
position:latlng,
map: map,
title: 'Hello World!'
});
/* I think you can use the jQuery like this within the showMap function? */
$.ajax({
url: 'phpmobile/getlanglong.php',
data: { "id": getacara },
dataType: 'json',
success: function( data, status ){
$.each( data, function( i,item ){
/* add a marker for each location in response data */
addMarker( item.latitude, item.longitude, 'A title ~ could be returned in json data' );
});
},
error: function(){
output.text('There was an error loading the data.');
}
});
}
/* simple function just to add a new marker */
function addMarker(lat,lng,title){
marker = new google.maps.Marker({
position:{lat:lat,lng:lng},
map: map,
title: title
});
}
document.addEventListener( 'DOMContentLoaded', showMap, false );
}());
</script>
</head>
<body>
<div id='map' style='width:600px;height:400px'></div>
</body>
</html>
正如您提到的,我在最終嘗試代碼時發現的一件事是,出現了一個與not a number
有關的錯誤,因此我使用parseFloat
來確保latlng對象文字獲取的是數字而不是字符串,從而做了一些小的更改。 下面是整個頁面的經過全面測試和正常工作的示例〜這是一個用於測試目的的all-in-one
頁面,當然,如果不進行編輯,數據庫詳細信息和表查找將無法直接為您工作。
<?php
if( $_SERVER['REQUEST_METHOD']=='GET' && isset( $_GET['ajax'] ) ){
$dbhost = 'localhost';
$dbuser = 'root';
$dbpwd = 'xx';
$dbname = 'xxx';
$db = new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );
$places=array();
$sql = 'select
`location_name` as \'name\',
`location_Latitude` as \'lat\',
`location_Longitude` as \'lng\'
from `maps`
limit 100';
$res = $db->query( $sql );
if( $res ) while( $rs=$res->fetch_object() ) $places[]=array( 'latitude'=>$rs->lat, 'longitude'=>$rs->lng, 'name'=>$rs->name );
$db->close();
header( 'Content-Type: application/json' );
echo json_encode( $places,JSON_FORCE_OBJECT );
exit();
}
?>
<!doctype html>
<html>
<head>
<meta charset='utf-8'/>
<title>Google Maps</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src='https://maps.google.com/maps/api/js' type='text/javascript'></script>
<script type='text/javascript'>
(function(){
var map,marker,latlng,bounds,infowin;
/* initial locations for map */
var _lat=56.61543329027024;
var _lng=-2.9266123065796137;
var getacara=0; /* What should this be? is it a function, a variable ???*/
function showMap(){
/* set the default initial location */
latlng={ lat: _lat, lng: _lng };
bounds = new google.maps.LatLngBounds();
infowin = new google.maps.InfoWindow();
/* invoke the map */
map = new google.maps.Map( document.getElementById('map'), {
center:latlng,
zoom: 10
});
/* show the initial marker */
marker = new google.maps.Marker({
position:latlng,
map: map,
title: 'Hello World!'
});
bounds.extend( marker.position );
/* I think you can use the jQuery like this within the showMap function? */
$.ajax({
/*
I'm using the same page for the db results but you would
change the below to point to your php script ~ namely
phpmobile/getlanglong.php
*/
url: document.location.href,/*'phpmobile/getlanglong.php'*/
data: { 'id': getacara, 'ajax':true },
dataType: 'json',
success: function( data, status ){
$.each( data, function( i,item ){
/* add a marker for each location in response data */
addMarker( item.latitude, item.longitude, item.name );
});
},
error: function(){
output.text('There was an error loading the data.');
}
});
}
/* simple function just to add a new marker */
function addMarker(lat,lng,title){
marker = new google.maps.Marker({/* Cast the returned data as floats using parseFloat() */
position:{ lat:parseFloat( lat ), lng:parseFloat( lng ) },
map:map,
title:title
});
google.maps.event.addListener( marker, 'click', function(event){
infowin.setContent(this.title);
infowin.open(map,this);
infowin.setPosition(this.position);
}.bind( marker ));
bounds.extend( marker.position );
map.fitBounds( bounds );
}
document.addEventListener( 'DOMContentLoaded', showMap, false );
}());
</script>
<style>
html, html body, #map{ height:100%; width:100%; padding:0; margin:0; }
</style>
</head>
<body>
<div id='map'></div>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.