使用谷歌地图网址查找地点 ID 或 GPS 坐标

问题描述 投票:0回答:1

我一直在寻找从谷歌地图共享网址检索 place_id 或直接检索某个地点的 GPS 坐标的解决方案。

我希望我的用户简单地复制粘贴位置链接,然后我检索他们想要的地方的坐标,这在谷歌地图的长网址上很容易完成,因为坐标是透明的(例如 https://www.google.com /地图/地点/埃菲尔铁塔/@48.8583701,2.2919064,17z/data=!3m1!4b1!4m6!3m5!1s0x47e66e2964e34e2d:0x8ddca9ee380ef7e0!8m2!3d48.8583701!4d2.2944813 !16zL20vMDJqODE?hl=en-FR&entry=ttu )。

但是当用户使用应用程序中更短的版本或地图上的共享选项时,我在尝试执行相同类型的操作时遇到麻烦(例如。https://maps.app.goo.gl/GmCgwbfgzPHq1JeHA )你知道这是否可能吗?我一直在尝试使用地理编码和地点详细信息 api...但我无法完全接近我想要的结果。

简而言之,如果可能,我想从这种格式下的链接获取 GPS 纬度/经度 https://maps.app.goo.gl/GmCgwbfgzPHq1JeHA 使用 PHP HTTP 请求,并将纬度/经度存储在数组中或至少按照这个网站做同样的事情https://wheregoes.com/


我的第一个猜测

函数 unshortenURL($shortURL) { $ch =curl_init($shortURL);

curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
$expandedURL = $shortURL;

if (!curl_errno($ch)) {
    $info = curl_getinfo($ch);
    if ($info['http_code'] == 301 || $info['http_code'] == 302) {
        $expandedURL = $info['url'];
    }
}

curl_close($ch);
return $expandedURL;

}

我的第二选择

函数 unshortenURL($mapsUrl) { //mapsUrl只是代码 https://maps.app.goo.gl/**GmCgwbfgzPHq1JeHA** 粗体 $apiURL = " https://places.googleapis.com/v1/places/$mapsUrl?fields=id,displayName&key=API_KEY";

$ch = curl_init($apiURL);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

curl_close($ch);

$data = json_decode($response, true);

if ($data && isset($data['results'][0]['geometry']['location'])) {
    $location = $data['results'][0]['geometry']['location'];
    $latitude = $location['lat'];
    $longitude = $location['lng'];
    return array('latitude' => $latitude, 'longitude' => $longitude);
} else {
    return null; // Invalid or incomplete response
}

}

{ “错误”: { “代码”:400, "message": "不是有效的地点 ID:GmCgwbfgzPHq1JeHA “, “状态”:“INVALID_ARGUMENT” } }

有道理,因为我试图用我认为是“id”的东西来获取id...

第三个选择基本相同,但带有地理编码

javascript php google-maps google-maps-api-3 geocoding
1个回答
0
投票

这是我找到的答案,对我自己很有用。

$pattern = '/@(-?\d+\.\d+),(-?\d+\.\d+)/';
    if(preg_match($pattern, $activityLink, $matches)){
      $locationX = $matches['1'];
      $locationY = $matches['2'];
    } else{
                if($_SERVER["REQUEST_METHOD"] == "POST"){
                    $redirects = checkRedirects($activityLink);
                    foreach($redirects as $url){
                        if(preg_match($pattern, $url, $matches)){
                  $locationX = $matches['1'];
                  $locationY = $matches['2'];
                            break;
                } else{
                            $address = extractAddressFromURL($url);
                            $coordinates = getLocationCoordinates($address);

                            if($coordinates){
                                $locationX = $coordinates['latitude'];
                      $locationY = $coordinates['longitude'];
                                break;
                            } else{
                            }
                        }
                    }
                } else{
                }
    }

这是我调用的函数:

function checkRedirects($url) {
  $redirects = [];
  $ch = curl_init($url);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

  $response = curl_exec($ch);
  $info = curl_getinfo($ch);
  $headers = substr($response, 0, $info['header_size']);

  $lines = explode("\n", $headers);

  foreach ($lines as $line) {
    if(stripos($line, "Location:") !== false) {
      $redirects[] = trim(str_ireplace("Location:", "", $line));
    }
  }

  curl_close($ch);

  return $redirects;
}

function extractAddressFromURL($url){
  $parsedUrl = parse_url($url);
  if(isset($parsedUrl['query'])){
    parse_str($parsedUrl['query'], $queryParameters);

    if(isset($queryParameters['q'])){
      $address = urldecode($queryParameters['q']);
    } elseif (isset($queryParameters['continue'])){
      $address = urldecode($queryParameters['continue']);
    } else{
      $address = 0;
    }
  } else{
    $address = 0;
  }

  return $address;
}

function getLocationCoordinates($adress){
  $encodedUrl = urlencode($adress);
  $apiURL = "https://maps.googleapis.com/maps/api/geocode/json?address=".$encodedUrl."&key=API_KEY";
  $ch = curl_init($apiURL);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $response = curl_exec($ch);
  curl_close($ch);
  $data = json_decode($response, true);

  if($data && isset($data['results'][0]['geometry']['location'])){
    $location = $data['results'][0]['geometry']['location'];
    $latitude = $location['lat'];
    $longitude = $location['lng'];
    return array('latitude' => $latitude, 'longitude' => $longitude);
  } else{
    return null;
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.