Reputation: 4847
what i am trying to achieve is basically mark the location of all the users logged into my chat which is another thing. so i though i'd use geolocation api (http://code.google.com/apis/gears/api_geolocation.html) and store the generated location in a session variable. but its not working. here is the code-`
<?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>`
and the problem is. that the variable seslatlng is set to (0,0) always which is not what i want. i am new to PHP and i only have rough idea how server side scripting works. can somebody please fix this?
Upvotes: 1
Views: 773
Reputation: 19751
The Best way is :
<script type="text/javascript">
var something=<?php echo json_encode($a); ?>;
</script>
Purpose of json_encode is to escpe the quotes and other entities <?php echo $a; ?>
can break your code.
Upvotes: 0
Reputation: 1388
Instead of using and for assigning values to javascript variables, use:
var thelat = '<?=$my_lat?>';
var thelong = '<?=$my_long?>';
and it will work for you. I have also tried it to assign php variables value to javascript variables.
Upvotes: 2
Reputation: 15984
when you are using jquery's ajax get function, what you wanna do is call a php function that only returns the locations. this is how is should look:
<?php
if ( $_GET['test'] ) {
$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;
} else {
?>
<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('My location is: ' + data);
});
}, 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>
<?php } ?>
Upvotes: 1
Reputation: 17434
I don't see anywhere that you are setting seslatlng. If you're wanting this to be a JS variable, add:
var seslatlng = "(<?php echo $my_lat . ',' . $my_lng; ?>)"; // (30.230, 8.030)
Upvotes: 1