update existing map

HI,
this is what i have done so far:
i created customs fields: "street", "apt #" , "City" , "State", "Zip code" and "Address". i did that because i am creating a directory site and later i will create a search by those fields. i am also using google maps plugin to display maps for the listings. i didn't want users to have to insert the full address twice (once for the fields and again for the map. so i created this function in functions.php :

function create_map() {
	global $post;
	$street_arg = get_post_meta($post->ID, 'Street' , TRUE);
	$apt_arg = get_post_meta($post->ID, 'Apt #' , TRUE);
	$city_arg = get_post_meta($post->ID, 'City' , TRUE);
	$state_arg = get_post_meta($post->ID, 'State' , TRUE);
	$zip_arg = get_post_meta($post->ID, 'zip code' , TRUE);
	$address_arg = $street_arg . " " . $apt_arg . " " . $city_arg . " " . $state_arg . " " . $zip_arg;
	update_post_meta($post->ID, 'Address', $address_arg);
}
add_action('edit_post', 'create_map');

after one inserts all the fields information and update the post, the information go into the value of "Address" field and displayed as a full address. this is also creates "agm_map_created" field and get a map value ( in the map plugin my settings are to use custom field support and in the address field the value is "Adress").

i am using the code below to display the map in a different place then inside the post area:

<?php $map_arg = '[map id ="'.get_post_meta($post->ID, 'agm_map_created', TRUE) .' "]' ; ?>
					<?php echo do_shortcode($map_arg); ?>

everything works fine, i use the fields i created to display a map but only when creating a new map.

problem is: when i am updating the info in one of the fields it is also updating it in the Address field. However, the map being displayed is still the first map i created. if i delete the "Address" and the "agm_map_created" fields and update the post it will create a new map which is fine. but what if i just want to update one of the fields and still update the map? can i?

    Vladislav

    Hi,

    Once the map is autocreated, it is generally only linked to a post through "agm_map_created" post meta field. The upside to this is that map only gets created once (performance), and it's then just another regular map that you can edit to your liking in map editor, include in map mashups etc. The downside is that it won't be updated if the post custom meta fields change, as you already noticed.

    Since you're updating the address field programmatically, the approach you already mentioned seems like the best idea to me. Whenever you change your address field, you can check for the presence of "agm_map_created" meta. If it's found, you may want to remove it from the post metas, and save your address. The Maps plugin should pick up from there and auto-create your new map. Depending on where in the execution process you're hooking up your function, this may happen either right away, or when somebody views map for the first time. Also, you may want to compare the new address field to the old version, to ensure you absolutely need to do this.

    If you want to completely remove the old map from the system, you can also use the "agm_map_created" field. This field actually holds the id of the autocreated map in the separate maps table, so you can remove that entry from the table too. You can either do it yourself, or instantiate the AgmMapModel class and use its' methods for this.

    I hope this helped at least a bit.

    fitoussi

    Thank you VeBailovity,
    i took your advice :

    Also, you may want to compare the new address field to the old version, to ensure you absolutely need to do this.

    now my function looks like that:

    ////////////// CREATE MAP USING ADDRESS FIELDS /////////////////////
    
    function create_map() {
    	global $post;
    	$street_arg = get_post_meta($post->ID, 'Street' , TRUE);       // strret
    	$apt_arg = get_post_meta($post->ID, 'Apt/Suit #' , TRUE);      //apt
    	$city_arg = get_post_meta($post->ID, 'City' , TRUE); 	       // city
    	$state_arg = get_post_meta($post->ID, 'State' , TRUE);         // state
    	$zip_arg = get_post_meta($post->ID, 'Zip Code' , TRUE);        // zip code
    	$address_arg = $street_arg . " " . $apt_arg . " " . $city_arg . " " . $state_arg . " " . $zip_arg;  // 
    
    	$old_address_arg = get_post_meta($post->ID, 'Address' , TRUE); 	// get old address
    	if ($old_address_arg != $address_arg) 				// compare old address to new address
    	delete_post_meta($post->ID, agm_map_created); 			// delete old address
    	update_post_meta($post->ID, 'Address' , $address_arg); 		// update new address
    }
    add_action('edit_post', 'create_map');
    
    ////////////////////////////////////////////////////////////////////////////////

    its works exactly how i wanted it to work. only thing i can't find how to do is how to delete the old map. i will keep trying but for the moment this is great.

    thanks again

    galaxy5
    ////////////// CREATE MAP USING ADDRESS FIELDS /////////////////////
    
    function create_map() {
    	global $post;
    	$street_arg = get_post_meta($post->ID, '_ct_text_4e2f36d674117' , TRUE);       // street name
    	$streetnum_arg = get_post_meta($post->ID, '_ct_text_4e47861306041' , TRUE);      //street number
    	$suburb_arg = get_post_meta($post->ID, '_ct_text_4e2f363d6dde8' , TRUE); 	       // suburb
    	$state_arg = get_post_meta($post->ID, '_ct_text_4e3126b162019' , TRUE);         // state
    	$zip_arg = get_post_meta($post->ID, '_ct_text_4e2f36537035d' , TRUE);        // zip code
    	$address_arg = $streetnum_arg  . " " . $street_arg . " " . $suburb_arg . " " . $state_arg . " " . $zip_arg;  // 
    
    	$old_address_arg = get_post_meta($post->ID, '_ct_text_4e478806c1015' , TRUE); 	// get old address
    	if ($old_address_arg != $address_arg) 				// compare old address to new address
    	delete_post_meta($post->ID, agm_map_created); 			// delete old address
    	update_post_meta($post->ID, '_ct_text_4e478806c1015' , $address_arg); 		// update new address
    }
    add_action('edit_post', 'create_map');
    
    ////////////////////////////////////////////////////////////////////////////////          
    
    }
    
    ?>

    I just placed this into my themes function,

    Now I just went into my listing post and added it information to the relevant fields.

    Clicked update but the address field is empty. Is it supposed to update automatically?

    galaxy5
    ////////////// CREATE MAP USING ADDRESS FIELDS /////////////////////
    
    function create_map() {
    	global $post;
    	$street_arg = get_post_meta($post->ID, '_ct_text_4e2f36d674117' , TRUE);       // street name
    	$streetnum_arg = get_post_meta($post->ID, '_ct_text_4e47861306041' , TRUE);      //street number
    	$suburb_arg = get_post_meta($post->ID, '_ct_text_4e2f363d6dde8' , TRUE); 	       // suburb
    	$state_arg = get_post_meta($post->ID, '_ct_text_4e3126b162019' , TRUE);         // state
    	$zip_arg = get_post_meta($post->ID, '_ct_text_4e2f36537035d' , TRUE);        // zip code
    	$address_arg = $streetnum_arg  . " " . $street_arg . " " . $suburb_arg . " " . $state_arg . " " . $zip_arg;  // 
    
    	$old_address_arg = get_post_meta($post->ID, 'address' , TRUE); 	// get old address
    	if ($old_address_arg != $address_arg) 				// compare old address to new address
    	delete_post_meta($post->ID, agm_map_created); 			// delete old address
    	update_post_meta($post->ID, 'address' , $address_arg); 		// update new address
    }
    add_action('edit_post', 'create_map');
    
    ////////////////////////////////////////////////////////////////////////////////

    I've changed it to this and still nothing happens to the custom field in address

    Vladislav

    Hi,

    The posts association is done relatively simple - it's basically an array of post IDs you wish to associate your map with. There's a filter you may want to use for this purpose, named "agm_google_maps-prepare_for_save-post_ids". The function bound to this hook will receive up to two parameters - the first one being the array of post IDs the plugin figured out this far (i.e. on its own), and the second one will be the raw map data. The function bound to this hook is expected to return a simple, flat array of post IDs to be associated with the map. I hope this helps, if you're inclined to so you may want to check the file lib/class_agm_map_model.php, method prepare_for_save() for a bit more in-depth info on exactly what's going on when a map gets saved.