address、city、stateとcountryを異なる変数で取得して、別々に表示できるようにしたいのですが、latlongが異なるため、どこかで住所全体を取得し、どこかだけを取得しています州と国ですので、latlongが変更されたため、特定の住所を取得できません。
これが私のコードです:
$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);
以下をお試しください:
<?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;
}
}
?>
Google Geocode APIを使用して、緯度と経度から場所を取得します。
ここでは、そのための単純な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:'';
上記のソースコードはここから見つかります- Google Maps APIとPHPを使用して緯度と経度から住所を取得します
私は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:
<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);
ここでGoogleGeocoding APIを確認してください: https://developers.google.com/maps/documentation/geocoding
あなたの場合、location_typeは "APPROXIMATE"に設定されています。これは、返される結果が正確なジオコードではないことを意味します。つまり、住所全体を取得することはできません。