如何利用php语法获取三维地图的坐标数据(将地址转换为经纬度坐标)

  

<?php

function getCoordinates($address) {
  $url = "http://api.map.baidu.com/geocoder/v2/?address=" . $address . "&output=json&ak={your_api_key}";
 
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
  $response = curl_exec($ch);
  curl_close($ch);
 
  $json = json_decode($response);
 
  if ($json->status === 0) {
    $location = $json->result->location;
    $lat = $location->lat;
    $lng = $location->lng;
 
    return array("lat" => $lat, "lng" => $lng);
  }
 
  return false;
}
 
$address = "杭州市西湖区文三路";
 
$coordinates = getCoordinates($address);
 
if ($coordinates) {
  echo "Latitude: " . $coordinates['lat'] . "<br/>";
  echo "Longitude: " . $coordinates['lng'] . "<br/>";
} else {
  echo "Geocoding failed!";
}
以上是编程学习网小编为您介绍的“如何利用php语法获取三维地图的坐标数据(将地址转换为经纬度坐标)”的全面内容,想了解更多关于 php入门 内容,请继续关注编程基础学习网。

相关文章