我想實現的是基本上標記所有登錄到我的聊天中的用戶的位置,這是另一回事。所以我雖然我會使用地理位置api(http://code.google.com/apis/gears/api_geolocation.html)並將生成的位置存儲在會話變量中。但它不起作用。這裏是代碼'在javascript中訪問php變量
<?php
$my_lat= $_GET['test']; // get data
$my_long= $_GET['fname'];
$my_name = "vivek";
$lat1=28.635308;
$long1=77.22496;
//$latitude = $_GET['latitude'];
//$longitude = $_GET['longitude'];// get data
echo $my_lat.''.$my_long;
?>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=''
&sensor=true"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var lat = position.coords.latitude;
var longi= position.coords.longitude;
var url = 'bam.php';
//post to the server!
$.get(url, { test: lat,fname: longi },function(data){
alert('data was passed!');
});
}, function() {
//now is when the marking should be done.
handleNoGeolocation(true);
});
}
});
</script>
<script type="text/javascript">
function initialize() {
var myLatlng = new google.maps.LatLng(28.635308,77.22496);
var myOptions = {
zoom: 4,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var thelat = '<?php echo $my_lat;?>';
var thelong = '<?php echo $my_long;?>';
var seslatlng = new google.maps.LatLng(thelat,thelong);
alert(seslatlng);
var marker = new google.maps.Marker({
position: seslatlng,
map: map,
animation: google.maps.Animation.DROP,
title:"Hello world!",
});
}
</script>`
問題是。變量seslatlng總是設置爲(0,0),這不是我想要的。我是PHP新手,我只是粗略的瞭解服務器端腳本是如何工作的。有人可以解決這個問題嗎?
1)當你運行echo $ my_lat時,你會得到什麼。''。$ my_long? 2)爲什麼您將地圖的中心設置爲硬編碼值,而不是基於客戶端的IP位置對其進行居中? –