@HAtan

Как импортировать адрес в ACF Google map?

Нужно импортировать 4к+ записей с бд, адрес есть, но долготы и широты нет, понимаю что надо как то геокодировать через google api. Но как конкретно это сделать?
Может кто знает какие то примеры или как правильно сделать?

Нашел такой пример для WP ALl Import, а я использую Really Simple CSV Importer, не знаю подойдет такой вариант или нет?
// Get location data from Google
function parse_address_google( $address = '' ) {
    if( empty( $address ) ) {
        return;
    }
    $geolocate_api_key = 'XXXX';
    $address = urlencode( $address );
    $request = wp_remote_get("https://maps.googleapis.com/maps/api/geocode/json?address=$address&key=$geolocate_api_key");
    $json = wp_remote_retrieve_body( $request );
    $data = json_decode( $json );
    if ( !$data ) {
		// ERROR! Google Maps returned an invalid response, expected JSON data
		return;
	}
	if ( isset($data->{'error_message'}) ) {
		// ERROR! Google Maps API returned an error
		return;
	}
	if ( empty($data->{'results'}[0]->{'geometry'}->{'location'}->{'lat'}) || empty($data->{'results'}[0]->{'geometry'}->{'location'}->{'lng'}) ) {
		// ERROR! Latitude/Longitude could not be found
		return;
	}
    $array = json_decode( $json, true );
    $result = $array['results'][0];
    $location = array();
    // street_number, street_name, city, state, post_code and country
    foreach ($result['address_components'] as $component) {
        switch ($component['types']) {
          case in_array('street_number', $component['types']):
            $location['street_number'] = $component['long_name'];
            break;
          case in_array('route', $component['types']):
            $location['street_name'] = $component['long_name'];
            break;
          case in_array('sublocality', $component['types']):
            $location['sublocality'] = $component['long_name'];
            break;
          case in_array('locality', $component['types']):
            $location['city'] = $component['long_name'];
            break;
          case in_array('administrative_area_level_2', $component['types']):
            $location['region'] = $component['long_name'];
            break;
          case in_array('administrative_area_level_1', $component['types']):
            $location['state'] = $component['long_name'];
            $location['state_short'] = $component['short_name'];
            break;
          case in_array('postal_code', $component['types']):
            $location['postal_code'] = $component['long_name'];
            break;
          case in_array('country', $component['types']):
            $location['country'] = $component['long_name'];
            $location['country_short'] = $component['short_name'];
            break;
        }
      }
      $location['lat'] = $data->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
	  $location['lng'] = $data->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
      return $location;
} 

// run on import
function _s_acf_update_map_field( $id ) {
    // Get ACF map field    
    $field = get_field( 'field_5eb49ed952662', $id );
    if( empty( $field['address'] ) ) {
        return;
    }
    $location = parse_address_google( $field['address'] );
    $args = wp_parse_args( $location, $field );
    update_field( 'field_5eb49ed952662', $args, $id );
}
add_action( 'pmxi_saved_post', '_s_acf_update_map_field', 10, 1 );


Тут нашел как импортировать, но всеравно нужно получить широту и долготу
https://gist.github.com/hissy/ebb1d317f9abc190b901
  • Вопрос задан
  • 54 просмотра
Пригласить эксперта
Ответы на вопрос 1
@ice_dog_as
Тот пример что вы нашли - работает для wp all import но только один адрес так сохраняет если в странице их несколько не робит(
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы