简体   繁体   English

PHP使用Google Geolocation API使用Geolocation提取并打印纬度和经度

[英]PHP Extract and print latitude and longitude using geolocation with google geolocation api

<?php echo $lat; ?>, 
<?php echo $lng; ?>) 

I'm trying to use the code from this page to get coordinates and dprint on the page above or below the maps using google, http://www.galengrover.com/projects/PHPGoogleMaps-Examples/geolocation.php 我正在尝试使用此页面中的代码,以使用Google http://www.galengrover.com/projects/PHPGoogleMaps-Examples/geolocation.php在地图上方或下方的页面上获取坐标和dprint

I don't seem to understand how this works. 我似乎不明白这是如何工作的。

require( '_system/autoload.php' );
$map_options = array(
'map_id'        => 'map23',
'draggable'     => true,
'center'        => 'San Diego, CA',
'height'        => '500px',
'width'         => '500px',
'zoom'          => 16,
'bicycle_layer' => true
);
$map = new \PHPGoogleMaps\Map( $map_options );

// 
$marker = \PHPGoogleMaps\Overlay\Marker::createFromUserLocation( array( 'geolocation_high_accuracy' => true, 'geolocation_timeout' => 10000 ) );

$map->addObject( $marker );

// If you want to set geolocation options you must call enableGeolocation() explicitly
// Otherwise it will be called for you when you use geolocation functions
$map->enableGeolocation( 5000, true );




// Set the loading content. This will display while the browser geolocates the user.
$map->setLoadingContent('<div style="background:#eee;height:300px;padding: 200px 0 0 0;text-align:center;"><img src="_images/loading.gif" style="display:block; margin: auto;"><p>Locating you...</p></div>'); ?>

<!DOCTYPE html>
<html lang="en">    
  <head>    
    <meta charset="utf-8">  
<title>Ground Overlays - 
  <?php echo PAGE_TITLE ?>
</title>    
<link rel="stylesheet" type="text/css" href="_css/style.css">       
<?php $map->printHeaderJS() ?>  
<?php $map->printMapJS() ?>
</head>
  <body><h1>Geolocation</h1>( 
    <?php require( '_system/nav.php' ) ?> 
<p>This example finds your location and centers the map on it. It also uses map::setLoadingContent() to display a loading message to the user.
</p>
<?php $map->printMap() ?>(
<?php echo $lat; ?>, 
<?php echo $lng; ?>) 

EDIT 编辑

I edited the code to show you this. 我编辑了代码以向您展示。 See geosuccess() function. 请参见geosuccess()函数。 It will alert your coordinates. 它将提醒您的坐标。 Everything is stored in "this" so if you go to the console in google chrome and type in "this" you will see all the data there. 一切都存储在“ this”中,因此,如果您使用google chrome浏览器进入控制台并输入“ this”,您将在其中看到所有数据。

<?php

// This is for my examples
require( '_system/config.php' );

// Autoload stuff
require( '_system/autoload.php' );

$map = new \PHPGoogleMaps\Map;

// 
$marker = \PHPGoogleMaps\Overlay\Marker::createFromUserLocation( array( 'geolocation_high_accuracy' => true, 'geolocation_timeout' => 10000 ) );

$map->addObject( $marker );

// If you want to set geolocation options you must call enableGeolocation() explicitly
// Otherwise it will be called for you when you use geolocation functions
$map->enableGeolocation( 5000, true );
$map->centerOnUser( \PHPGoogleMaps\Service\Geocoder::geocode('New York, NY') );
$map->setWidth('500px');
$map->setHeight('500px');
$map->setZoom(16);

// Set the callbacks
$map->setGeolocationFailCallback( 'geofail' );
$map->setGeolocationSuccessCallback( 'geosuccess' );

// Set the loading content. This will display while the browser geolocates the user.
$map->setLoadingContent('<div style="background:#eee;height:300px;padding: 200px 0 0 0;text-align:center;"><img src="_images/loading.gif" style="display:block; margin: auto;"><p>Locating you...</p></div>');
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Geolocation - <?php echo PAGE_TITLE ?></title>
    <link rel="stylesheet" type="text/css" href="_css/style.css">
    <?php $map->printHeaderJS() ?>
    <?php $map->printMapJS() ?>
    <script type="text/javascript">
    function geofail() {
        alert( 'geolocation failed' );
    }
    function geosuccess() {
        alert("Lat:" + this.geolocation.hb + " Long: " + this.geolocation.ib);
        //alert( 'geolocation succeeded' );
    }
    </script>
</head>
<body>

<h1>Geolocation</h1>
<?php require( '_system/nav.php' ) ?>

<p>This example finds your location and centers the map on it. It also uses map::setLoadingContent() to display a loading message to the user.</p>

<?php $map->printMap() ?>

</body>

</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM