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

2024年07月16日 建站教程

<?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!";
}

本文链接:http://so.lmcjl.com/news/8599/

展开阅读全文
相关内容