Wor*_*sor 12 php google-maps geocoding
是否可以转换(在PHP后端):
<?php
$Address = "Street 1, City, Country"; // just normal address
?>
Run Code Online (Sandbox Code Playgroud)
至
<?php
$LatLng = "10.0,20.0"; // latitude & lonitude
?>
Run Code Online (Sandbox Code Playgroud)
我目前使用的代码是默认代码:
<script type='text/javascript' src='https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false'></script>
<script>
function initialize() {
var myLatlng = new google.maps.LatLng(10.0,20.0);
var mapOptions = {
zoom: 16,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map'), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: 'This is a caption'
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<div id="map"></div>
Run Code Online (Sandbox Code Playgroud)
我已经阅读了https://developers.google.com/maps/documentation/geocoding一段时间了,但我觉得我没经验.
谢谢.
Nei*_*son 34
这对我有用:
<?php
$Address = urlencode($Address);
$request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$Address."&sensor=true";
$xml = simplexml_load_file($request_url) or die("url not loading");
$status = $xml->status;
if ($status=="OK") {
$Lat = $xml->result->geometry->location->lat;
$Lon = $xml->result->geometry->location->lng;
$LatLng = "$Lat,$Lon";
}
?>
Run Code Online (Sandbox Code Playgroud)
参考文献:
小智 7
<?php
/*
* Given an address, return the longitude and latitude using The Google Geocoding API V3
*
*/
function Get_LatLng_From_Google_Maps($address) {
$address = urlencode($address);
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=false";
// Make the HTTP request
$data = @file_get_contents($url);
// Parse the json response
$jsondata = json_decode($data,true);
// If the json data is invalid, return empty array
if (!check_status($jsondata)) return array();
$LatLng = array(
'lat' => $jsondata["results"][0]["geometry"]["location"]["lat"],
'lng' => $jsondata["results"][0]["geometry"]["location"]["lng"],
);
return $LatLng;
}
/*
* Check if the json data from Google Geo is valid
*/
function check_status($jsondata) {
if ($jsondata["status"] == "OK") return true;
return false;
}
/*
* Print an array
*/
function d($a) {
echo "<pre>";
print_r($a);
echo "</pre>";
}
Run Code Online (Sandbox Code Playgroud)
有关如何使用上述功能的示例代码,请随时访问我的博客
是的你可以.你会做的请求与file_get_contents或curl到以下网址:
http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false
返回的是json编码的响应.您可以使用将其解析为数组json_decode.
文档页面有一个响应可能的样子,用它来查找所需的数据.