谷歌地图API问题

发布于 2025-01-08 13:29:12 字数 2068 浏览 9 评论 0原文

我有一些代码可以接受任何地址并返回纬度和经度作为 php 变量。

if ($_SESSION['where']) {
    $where = stripslashes($_SESSION['where']);
    $whereurl = urlencode($where);

$location = file("http://maps.google.com/maps/geo?q=new+york+New+york
&output=csv&key=xxxxxxxxxxxxxxxxxxxxxxxx");
        list ($stat,$acc,$north,$east) = explode(",",$location[0]);
        $html = "Information for ".$where."<br>";
        $html .= "North: $north, East: $east<br>";
        $html .= "Accuracy: $acc, Status: $stat<br>";
} else {
        $html = "Varibles Undefined";
}

?>


     <?php $_SESSION['lat'] = "$html"; $_SESSION['lon'] = "$whereurl"; echo"<strong>"?><?php echo "$north";?>&deg; North, <?php echo "$east";?>&deg; East</strong>

我知道它有效,因为当我

http://maps.google.com/maps/geo?q=$whereurl
    &output=csv&key=xxxxxxxxxxxxxxxxxxxx

手动输入浏览器时,它会返回

200,4,40.7143528,-74.0059731

我需要保存为 PHP 变量的内容。但是它不会将其保存为 $north 或 $east。关于如何解决这个问题有什么建议吗?提前致谢。

这就是它给我的:

{ "name": "new york new york", "Status": { "code": 200, "request": "geocode" }, "Placemark": [ { "id": "p1 ", "地址": "美国纽约州纽约", "AddressDetails": { "准确性" : 4, "国家" : { "行政区域" : { "AdministrativeAreaName" : "NY", "SubAdministrativeArea" : { "Locality" : { "LocalityName" : "New York" }, "SubAdministrativeAreaName" : "New York" } }, "CountryName" : "USA", " CountryNameCode" : "美国" } }, "扩展数据": { "LatLonBox": { “北”:40.8495342,“南”:40.5788964,“东”:-73.7498543,“西”:-74.2620919 } },“点”:{“坐标”:[-74.0059731, 40.7143528, 0 ] } }, { “id”:“p2”,“地址”:“美国纽约州曼哈顿”,“AddressDetails”:{“准确性”:4,“国家/地区”:{“管理区域”:{“管理区域名称”:“纽约", "子行政区域" : { "地区" : { "DependentLocality" : { "DependentLocalityName" : "曼哈顿" }, "LocalityName" : "纽约" }, "SubAdministrativeAreaName" : "纽约" } }, "CountryName" : "美国", "CountryNameCode" : "美国" } }, "扩展数据": { " LatLonBox": { "北": 40.8200450, "南": 40.6980780, "东": -73.9033130, "西": -74.0351490 } }, "点": { "坐标": [ -73.9662495, 40.7834345, 0 ] } } ] }

I've got some code that takes any address and returns lat and long as a php variable.

if ($_SESSION['where']) {
    $where = stripslashes($_SESSION['where']);
    $whereurl = urlencode($where);

$location = file("http://maps.google.com/maps/geo?q=new+york+New+york
&output=csv&key=xxxxxxxxxxxxxxxxxxxxxxxx");
        list ($stat,$acc,$north,$east) = explode(",",$location[0]);
        $html = "Information for ".$where."<br>";
        $html .= "North: $north, East: $east<br>";
        $html .= "Accuracy: $acc, Status: $stat<br>";
} else {
        $html = "Varibles Undefined";
}

?>


     <?php $_SESSION['lat'] = "$html"; $_SESSION['lon'] = "$whereurl"; echo"<strong>"?><?php echo "$north";?>° North, <?php echo "$east";?>° East</strong>

I know it works because when I enter

http://maps.google.com/maps/geo?q=$whereurl
    &output=csv&key=xxxxxxxxxxxxxxxxxxxx

manually into a browser, it returns

200,4,40.7143528,-74.0059731

which is what I need saved as a PHP variable. However it's not saving it as $north or $east. Any suggestions on how to fix this? Thank's in advance.

here's what it gives me:

{ "name": "new york new york", "Status": { "code": 200, "request": "geocode" }, "Placemark": [ { "id": "p1", "address": "New York, NY, USA", "AddressDetails": { "Accuracy" : 4, "Country" : { "AdministrativeArea" : { "AdministrativeAreaName" : "NY", "SubAdministrativeArea" : { "Locality" : { "LocalityName" : "New York" }, "SubAdministrativeAreaName" : "New York" } }, "CountryName" : "USA", "CountryNameCode" : "US" } }, "ExtendedData": { "LatLonBox": { "north": 40.8495342, "south": 40.5788964, "east": -73.7498543, "west": -74.2620919 } }, "Point": { "coordinates": [ -74.0059731, 40.7143528, 0 ] } }, { "id": "p2", "address": "Manhattan, New York, NY, USA", "AddressDetails": { "Accuracy" : 4, "Country" : { "AdministrativeArea" : { "AdministrativeAreaName" : "NY", "SubAdministrativeArea" : { "Locality" : { "DependentLocality" : { "DependentLocalityName" : "Manhattan" }, "LocalityName" : "New York" }, "SubAdministrativeAreaName" : "New York" } }, "CountryName" : "USA", "CountryNameCode" : "US" } }, "ExtendedData": { "LatLonBox": { "north": 40.8200450, "south": 40.6980780, "east": -73.9033130, "west": -74.0351490 } }, "Point": { "coordinates": [ -73.9662495, 40.7834345, 0 ] } } ] }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尝蛊 2025-01-15 13:29:12

使用 file_get_contents 代替(它返回一个字符串)。

简短示例:

$location = file_get_contents("http://maps.google.com/maps/geo?q=$whereurl&output=csv");
$test = explode(",",$location);
print_r($test);

完整解决方案在这里:

http://www.ece.uvic .ca/~casualt/random.php?location=Bermuda

来源:

$theLocation = "new+york";
$locIn = $_GET['location'];
if($locIn)
{
    $theLocation = urlencode($locIn);   //Just to be safe (even though this got passed in via get
}
$location = file_get_contents('http://maps.google.com/maps/geo?q='.$theLocation.'&output=csv&key=AIzaSyAQtiJHUt5KiL-aGONLlACQQ3OdwpbvFzI');
$test = explode(",",$location);
//print_r($test);
$lat = $test[2];
$long = $test[3];
echo '<h1>Magical Location of '.$theLocation.':</h1>';
echo '<ul>';
echo '<li>Longitude: '.$long.'</li>';
echo '<li>Latitude: '.$lat.'</li>';
echo '</ul>';

这应该可以满足您的要求。 :)

Use file_get_contents instead (which returns you a string).

Short Example:

$location = file_get_contents("http://maps.google.com/maps/geo?q=$whereurl&output=csv");
$test = explode(",",$location);
print_r($test);

Full Solution here:

http://www.ece.uvic.ca/~casualt/random.php?location=Bermuda

Source:

$theLocation = "new+york";
$locIn = $_GET['location'];
if($locIn)
{
    $theLocation = urlencode($locIn);   //Just to be safe (even though this got passed in via get
}
$location = file_get_contents('http://maps.google.com/maps/geo?q='.$theLocation.'&output=csv&key=AIzaSyAQtiJHUt5KiL-aGONLlACQQ3OdwpbvFzI');
$test = explode(",",$location);
//print_r($test);
$lat = $test[2];
$long = $test[3];
echo '<h1>Magical Location of '.$theLocation.':</h1>';
echo '<ul>';
echo '<li>Longitude: '.$long.'</li>';
echo '<li>Latitude: '.$lat.'</li>';
echo '</ul>';

That should do the trick for you. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文