我现在正在寻找解决方案。所以我希望有人能够帮助我:
我得到谷歌api的地址,我知道谷歌的结果页面很好。但是应该保存到数据库中并且不起作用。以下代码中似乎存在错误。如果有人找到它我会很高兴...;)
非常感谢!
function reverse_geocode($latitude, $longitude) {
$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=".$latitude.",".$longitude."&sensor=false";
$result = wp_remote_get($url);
$json = json_decode($result['body']);
foreach ($json->results as $result)
{
foreach($result->address_components as $addressPart) {
if((in_array('locality', $addressPart->types)) && (in_array('political', $addressPart->types)))
$city = $addressPart->long_name;
else if((in_array('administrative_area_level_1', $addressPart->types)) && (in_array('political', $addressPart->types)))
$state = $addressPart->long_name;
else if((in_array('country', $addressPart->types)) && (in_array('political', $addressPart->types)))
$country = $addressPart->long_name;
}
}
if(($city != '') && ($state != '') && ($country != ''))
$address = $city.', '.$state.', '.$country;
else if(($city != '') && ($state != ''))
$address = $city.', '.$state;
else if(($state != '') && ($country != ''))
$address = $state.', '.$country;
else if($country != '')
$address = $country;
return $address;
}
然后它(通常)写入DB:
function geolocation_save_postdata($post_id) {
// Check authorization, permissions, autosave, etc
if (!wp_verify_nonce($_POST['geolocation_nonce'], plugin_basename(__FILE__)))
return $post_id;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
if('page' == $_POST['post_type'] ) {
if(!current_user_can('edit_page', $post_id))
return $post_id;
} else {
if(!current_user_can('edit_post', $post_id))
return $post_id;
}
$latitude = clean_coordinate($_POST['geolocation-latitude']);
$longitude = clean_coordinate($_POST['geolocation-longitude']);
$address = reverse_geocode($latitude, $longitude);
$public = $_POST['geolocation-public'];
$on = $_POST['geolocation-on'];
if((clean_coordinate($latitude) != '') && (clean_coordinate($longitude)) != '') {
update_post_meta($post_id, 'geo_latitude', $latitude);
update_post_meta($post_id, 'geo_longitude', $longitude);
if(esc_html($address) != '') {
update_post_meta($post_id, 'geo_address', $address);
} else {
update_post_meta($post_id, 'geo_address', 'unbekannter Adresse | Koordinaten: '.$latitude.' Breite, '.$longitude.' Länge');
}
if($on) {
update_post_meta($post_id, 'geo_enabled', 1);
if($public)
update_post_meta($post_id, 'geo_public', 1);
else
update_post_meta($post_id, 'geo_public', 0);
}
else {
update_post_meta($post_id, 'geo_enabled', 0);
update_post_meta($post_id, 'geo_public', 1);
}
}
return $post_id;
}