【问题标题】:PHP Extract and print latitude and longitude using geolocation with google geolocation apiPHP 使用 google geolocation api 使用地理位置提取和打印纬度和经度
【发布时间】:2013-04-13 18:37:03
【问题描述】:
<?php echo $lat; ?>, 
<?php echo $lng; ?>) 

我正在尝试使用此页面中的代码使用谷歌在地图上方或下方的页面上获取坐标和 dprint, http://www.galengrover.com/projects/PHPGoogleMaps-Examples/geolocation.php

我似乎不明白这是如何工作的。

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; ?>) 

【问题讨论】:

  • 为什么不直接使用纯javascript?

标签: php google-maps-api-3


【解决方案1】:

编辑

我编辑了代码来向您展示这一点。请参阅 geosuccess() 函数。它会提醒你的坐标。一切都存储在“this”中,因此如果您在谷歌浏览器中进入控制台并输入“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>

【讨论】:

  • 非常感谢,我正在寻找的是 PHP 中的代码,或者如果有人可以告诉我如何使用 (position.coords.latitude, position.coords.longitude) 会有所帮助PHP.
  • 好的,我刚刚安装了您正在使用的插件,它工作正常 :) 那么问题是什么?
  • 使用该插件在地图下方的同一页面上显示坐标值。
  • 是的,但是你卡在哪里了?你试过什么?插件在运行吗?如果您详细说明该问题,我只能为您提供帮助。
  • 脚本可以检测到我的位置并将其显示在地图上。我正在使用 , ) 来查找纬度和经度值,我只得到这个 (,)
猜你喜欢
  • 2011-03-10
  • 2018-07-26
  • 1970-01-01
  • 1970-01-01
  • 2011-03-30
  • 2013-03-20
  • 2012-07-24
  • 2018-02-15
  • 1970-01-01
相关资源
最近更新 更多