从 PHP 中的经纬度获取位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29624114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get location from latitude and longitude in PHP
提问by Zarif
I want to get address, city, stateand countryin different variables so that I can display it separately but due to different latlong, somewhere I am getting whole address and somewhere only state and country, so I am not able to get a specific address due to changing latlong.
我想在不同的变量中获取地址、城市、州和国家/地区,以便我可以单独显示它,但由于不同的经纬度,我在某处获得完整地址,而在某处仅获得州和国家,因此我无法获得特定地址由于改变latlong。
Here is my code:
这是我的代码:
$geoLocation=array();
$URL = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=8.407168,6.152344&sensor=false';
$data = file_get_contents($URL);
$geoAry = json_decode($data,true);
for($i=0;$i<count($geoAry['results']);$i++){
if($geoAry['results'][$i]['types'][0]=='sublocality_level_1'){
$address=$geoAry['results'][$i]['address_components'][0]['long_name'];
$city=$geoAry['results'][$i]['address_components'][1]['long_name'];
$state=$geoAry['results'][$i]['address_components'][3]['long_name'];
$country=$geoAry['results'][$i]['address_components'][4]['long_name'];
break;
}
else {
$address=$geoAry['results'][0]['address_components'][2]['long_name'];
$city=$geoAry['results'][0]['address_components'][3]['long_name'];
$state=$geoAry['results'][0]['address_components'][5]['long_name'];
$country=$geoAry['results'][0]['address_components'][6]['long_name'];
}
}
$geoLocation = array(
'city'=>$city,
'state'=>$state,
'country'=>$country,
'address'=>$address
);
print_r($geoLocation);
回答by SHAZ
Try Below one:
尝试以下之一:
<?php
$geolocation = $latitude.','.$longitude;
$request = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.$geolocation.'&sensor=false';
$file_contents = file_get_contents($request);
$json_decode = json_decode($file_contents);
if(isset($json_decode->results[0])) {
$response = array();
foreach($json_decode->results[0]->address_components as $addressComponet) {
if(in_array('political', $addressComponet->types)) {
$response[] = $addressComponet->long_name;
}
}
if(isset($response[0])){ $first = $response[0]; } else { $first = 'null'; }
if(isset($response[1])){ $second = $response[1]; } else { $second = 'null'; }
if(isset($response[2])){ $third = $response[2]; } else { $third = 'null'; }
if(isset($response[3])){ $fourth = $response[3]; } else { $fourth = 'null'; }
if(isset($response[4])){ $fifth = $response[4]; } else { $fifth = 'null'; }
if( $first != 'null' && $second != 'null' && $third != 'null' && $fourth != 'null' && $fifth != 'null' ) {
echo "<br/>Address:: ".$first;
echo "<br/>City:: ".$second;
echo "<br/>State:: ".$fourth;
echo "<br/>Country:: ".$fifth;
}
else if ( $first != 'null' && $second != 'null' && $third != 'null' && $fourth != 'null' && $fifth == 'null' ) {
echo "<br/>Address:: ".$first;
echo "<br/>City:: ".$second;
echo "<br/>State:: ".$third;
echo "<br/>Country:: ".$fourth;
}
else if ( $first != 'null' && $second != 'null' && $third != 'null' && $fourth == 'null' && $fifth == 'null' ) {
echo "<br/>City:: ".$first;
echo "<br/>State:: ".$second;
echo "<br/>Country:: ".$third;
}
else if ( $first != 'null' && $second != 'null' && $third == 'null' && $fourth == 'null' && $fifth == 'null' ) {
echo "<br/>State:: ".$first;
echo "<br/>Country:: ".$second;
}
else if ( $first != 'null' && $second == 'null' && $third == 'null' && $fourth == 'null' && $fifth == 'null' ) {
echo "<br/>Country:: ".$first;
}
}
?>
回答by JoyGuru
Use Google Geocode API to get the location from latitude and longitude.
使用 Google Geocode API 从纬度和经度获取位置。
Here the simple PHP script for that.
这里是简单的 PHP 脚本。
$latitude = 'Insert Latitude';
$longitude = 'Insert Longitude';
$geocodeFromLatLong = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($latitude).','.trim($longitude).'&sensor=false');
$output = json_decode($geocodeFromLatLong);
$status = $output->status;
$address = ($status=="OK")?$output->results[1]->formatted_address:'';
The above source code is found from here - Get Address from Latitude and Longitude using Google Maps API and PHP
上面的源代码是从这里找到的 - Get Address from Latitude and Longitude using Google Maps API and PHP
回答by Dr. Z
Check the Google Geocoding API here : https://developers.google.com/maps/documentation/geocoding
在此处检查 Google Geocoding API:https: //developers.google.com/maps/documentation/geocoding
In your case, the location_type is set to "APPROXIMATE" It means that the returned result is not a precise geocode, which means that you will not have the whole address.
在您的情况下,location_type 设置为“APPROXIMATE”这意味着返回的结果不是精确的地理编码,这意味着您不会拥有完整的地址。
回答by FahadKhan
I am using codeigniter and have similar requirements this is how i get it
我正在使用 codeigniter 并且有类似的要求,这就是我得到它的方式
<script>
var lt=pos.lat;
var lon=pos.lng;
var current_lat=$("#current_lat").val(lt);
var current_long=$("#current_long").val(lon);
</script>
HTML:
HTML:
<input type="hidden" id="current_lat" name="current_lat" value="<?php echo $this-
>session->userdata('current_lat');?>">
<input type="hidden" id="current_long" name="current_long"
value="<?php echo $this->session->userdata('current_long');?>">
Php:
博士:
$current_long= $this->input->post('current_long');
$current_lat=$this->input->post('current_lat');
$newdata = array(
'current_lat' => $current_lat,
'current_long' => $current_long
);
$this->session->set_userdata($newdata);
回答by Jewel
The above solutions will not work because google is now required API KEY to getting the data:
上述解决方案将不起作用,因为谷歌现在需要 API KEY 来获取数据:
Here is the a simple function that will return lat
, long
along with google_place_id
这是一个将返回的简单函数lat
,long
以及google_place_id
function geoLocate($address)
{
try {
$lat = 0;
$lng = 0;
$data_location = "https://maps.google.com/maps/api/geocode/json?key=".$GOOGLE_API_KEY_HERE."&address=".str_replace(" ", "+", $address)."&sensor=false";
$data = file_get_contents($data_location);
usleep(200000);
// turn this on to see if we are being blocked
// echo $data;
$data = json_decode($data);
if ($data->status=="OK") {
$lat = $data->results[0]->geometry->location->lat;
$lng = $data->results[0]->geometry->location->lng;
if($lat && $lng) {
return array(
'status' => true,
'lat' => $lat,
'long' => $lng,
'google_place_id' => $data->results[0]->place_id
);
}
}
if($data->status == 'OVER_QUERY_LIMIT') {
return array(
'status' => false,
'message' => 'Google Amp API OVER_QUERY_LIMIT, Please update your google map api key or try tomorrow'
);
}
} catch (Exception $e) {
}
return array('lat' => null, 'long' => null, 'status' => false);
}