PHP Classes
elePHPant
Icontem

Get current location Lat Long

Recommend this page to a friend!

      Map Builder  >  All threads  >  Get current location Lat Long  >  (Un) Subscribe thread alerts  
Subject:Get current location Lat Long
Summary:$map->addGeoMarker and stuff
Messages:14
Author:Samuel Bistak
Date:2015-12-18 17:54:22
 
  1 - 10   11 - 14  

  1. Get current location Lat Long   Reply   Report abuse  
Samuel Bistak - 2015-12-18 17:54:22
Dear fellas,

I'm using this class is very good but i have one problem. How i can get current location Lat. and Long. when i first load map?

  2. Re: Get current location Lat Long   Reply   Report abuse  
Vagharshak Tozalakyan - 2015-12-19 13:44:48 - In reply to message 1 from Samuel Bistak
Currently there is no JS callback for geolocation, but you can use the JS timer to detect when coordinates available. Something like this:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Maps Test</title>
<script>
function mbOnAfterInit(map) {
var timer = window.setInterval(function() {
if (geoMarker.getPosition()) {
alert(geoMarker.getPosition());
clearInterval(timer);
}
}, 1000);
}
</script>
</head>
<body>
<?php
require_once 'class.MapBuilder.php';
$map = new MapBuilder();
$map->overrideCenterByGeo(true);
$map->addGeoMarker();
$map->show();
?>
</body>
</html>

  3. Re: Get current location Lat Long   Reply   Report abuse  
Samuel Bistak - 2015-12-19 13:54:54 - In reply to message 2 from Vagharshak Tozalakyan
Thanks a lot. But i need to set variable in PHP for current Lat and Long coordinate to work with them that's why alert is not very helpful. Don't you have any idea how to manage that?

  4. Re: Get current location Lat Long   Reply   Report abuse  
Samuel Bistak - 2015-12-19 14:12:27 - In reply to message 3 from Samuel Bistak
Sorry never mind. I got it now with $map->GetcenterLat / Lng;

Thx

  5. Re: Get current location Lat Long   Reply   Report abuse  
Vagharshak Tozalakyan - 2015-12-19 14:51:47 - In reply to message 4 from Samuel Bistak
The latest version of library includes corresponding JS callback:

<script>
function mbOnAfterLocationDetected(lat, lng) {
alert(lat + ' ' + lng);
}
</script>

It is impossible to get client coordinates directly in PHP simply because they are CLIENT coordinates. However, you can pass them back via AJAX call:

<script>
function mbOnAfterLocationDetected(lat, lng) {
$.post('myscript.php', {lat: lat, lng: lng}, function(response) {
});
}
</script>



  6. Re: Get current location Lat Long   Reply   Report abuse  
Samuel Bistak - 2015-12-19 15:02:00 - In reply to message 5 from Vagharshak Tozalakyan
I used $map->$map->getCenterLat(); after <script>...</script> and it worked for now...

  7. Re: Get current location Lat Long   Reply   Report abuse  
Vagharshak Tozalakyan - 2015-12-19 16:00:03 - In reply to message 6 from Samuel Bistak
The getCenterLat() and getCenterLng() methods are just return what was set by setCenter(), nothing more... So they cannot be used to detect client location coordinates.

  8. Re: Get current location Lat Long   Reply   Report abuse  
Samuel Bistak - 2015-12-19 17:25:11 - In reply to message 7 from Vagharshak Tozalakyan
Aah ok sorry my bad :)...

Please can you explain me more how can i get the values from current position Lat and Lng via Ajax?

  9. Re: Get current location Lat Long   Reply   Report abuse  
Vagharshak Tozalakyan - 2015-12-19 18:31:39 - In reply to message 1 from Samuel Bistak
Just be sure to use the latest version of the class that support mbOnAfterLocationDetected() callback.

<?php
// myscript.php
if (isset($_POST['lat']) && isset($_POST['lng'])) {
$lat = floatval($_POST['lat']);
$lng = floatval($_POST['lng']);
// do something...
die();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Maps Test</title>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
function mbOnAfterLocationDetected(lat, lng) {
$.post("myscript.php", {lat: lat, lng: lng});
}
</script>
</head>
<body>
<?php
require_once 'class.MapBuilder.php';
$map = new MapBuilder();
$map->overrideCenterByGeo(true);
$map->addGeoMarker();
$map->show();
?>
</body>
</html>

  10. Re: Get current location Lat Long   Reply   Report abuse  
Samuel Bistak - 2015-12-19 19:20:17 - In reply to message 9 from Vagharshak Tozalakyan
Thanks a lot. I have updated class but somthing is wrong.
When i want make var_dump($lat); is still empty but AJAX is making request and i can find coordinates in network tab they are there but i cannot use them in document. cuz $_POST['lat'] is empty... i don't know why...

if (isset($_POST['lat']) && isset($_POST['lng'])) {
$lat = floatval($_POST['lat']);
$lng = floatval($_POST['lng']);

var_dump($lng);
die();
}
else {

echo 'empty';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Maps Test</title>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
function mbOnAfterLocationDetected(lat, lng) {
$.post("map.php", {lat: lat, lng: lng});
}
</script>
</head>
<body>
<?php
require_once 'class.MapBuilder.php';
$map = new MapBuilder();
$map->overrideCenterByGeo(true);
$map->addGeoMarker();
$map->show();
?>
</body>
</html>

 
  1 - 10   11 - 14