Support

Account

Home Forums Bug Reports Google Maps field normalization Reply To: Google Maps field normalization

  • Just thought I would add too this post.

    I had this same issue but did not want to deal with keeping the normalized postmeta fields _latitude and _longitude in sync with the ACF postmeta.

    This query “deserializes” the ACF google_maps field so no normalization is required. Obviously it would be better if ACF map field was just normalized by the plugin and not stored as serialized PHP object.

    
    function get_nearest_location_page($latitude, $longitude, $maxdistance = 100) {
    	global $wpdb;
    	$location_query = <<<EOF
    	SELECT wp_posts.post_title as post_title,
    	wp_posts.ID as post_id,
    	( 3959 * acos(
    	cos( radians( %f ) ) *
    	cos( radians( CONVERT(REPLACE(SUBSTRING_INDEX(SUBSTRING_INDEX(location.meta_value,';',4),':',-1),'"',''),  DECIMAL( 10, 6 )) ) ) *
    	cos( radians( CONVERT(REPLACE(SUBSTRING_INDEX(SUBSTRING_INDEX(location.meta_value,';',6),':',-1),'"',''),  DECIMAL( 10, 6 ))  ) - radians( %f ) ) +
    	sin( radians( %f ) ) * sin( radians( CONVERT(REPLACE(SUBSTRING_INDEX(SUBSTRING_INDEX(location.meta_value,';',4),':',-1),'"',''),  DECIMAL( 10, 6 ))  ) )
    	) ) AS distance
    	FROM wp_postmeta as location, wp_posts
    	WHERE wp_posts.post_type = "page"
    	AND wp_posts.ID = location.post_id
    	AND location.meta_key LIKE "service_locations_%%"
    	HAVING distance < %f
    	ORDER BY distance ASC
    	LIMIT 1
    EOF;
    	$location_query_string = $wpdb->prepare($location_query, $latitude, $longitude, $latitude, $maxdistance);
    	return $wpdb->get_results($location_query_string, ARRAY_A);
    }
    

    in this case my field is actually an ACF Pro repeater field named service_locations.