Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Realtime #158

Open
htikeaung opened this issue Dec 23, 2019 · 0 comments
Open

Realtime #158

htikeaung opened this issue Dec 23, 2019 · 0 comments

Comments

@htikeaung
Copy link

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,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant