-
Notifications
You must be signed in to change notification settings - Fork 167
Description
Hi,
I'm a beginner of Javascript and trying to use leaflet-realtime. From your library I replaced https://wanderdrone.appspot.com/ with my url and I try with php return like
$lat = 16.913213;
$lon = 98.232323;
$latLon = array($lon,$lat);
$myLocation->geometry->type = "Point";
$myLocation->geometry->coordinates = $latLon;
$myLocation->type = "Feature";
$myLocation->properties = new stdClass();
$location = json_encode($myLocation);
echo $location;
it work fine. But as you know since php is server side language and can't get user's real geolocation. So I try with Javascript and trying many way to return from html5 built-in api navigator.geolocation.getCurrentPosition(callback,fail) like this
request.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ if(navigator.geolocation){ navigator.geolocation.getCurrentPosition(showLocation); }else{ $('#location').html('Geolocation is not supported by this browser.'); } }); function showLocation(position){ var latitude = position.coords.latitude; var longitude = position.coords.longitude; $.ajax({ type:'POST', url:'response.php', dataType: 'json', data:'latitude='+latitude+'&longitude='+longitude, success:function(msg){ if(msg){ msg = JSON.stringify(msg); console.log(msg); }else{ alert('Error'); } } }); } </script>response.php
if(!empty($_POST['latitude']) && !empty($_POST['longitude'])){
$lat = $_POST['latitude'];
$lon = $_POST['longitude'];
$latLon = array($lon,$lat);
$myLocation->geometry->type = "Point";
$myLocation->geometry->coordinates = $latLon;
$myLocation->type = "Feature";
$myLocation->properties = new stdClass();
$location = json_encode($myLocation);
echo $location;
}
But leaflet-realtime show me this error
'leaflet-realtime.js:260 SyntaxError: Unexpected token < in JSON at position 2 undefined'
I have no idea how to return JSON string with Javascript. I also try with fetch but I can't get it. Could you please help me about this issue?
Htike Aung,
Web Developer,