Center the map on a specific marker on page load


In this tutorial, you’ll see how to center the map on a specific marker when loading the map page.

  1. For this to work, you’ll have to add the attribute post_id to the URL. For example, if you want to navigate from your website’s homepage www.website.com to your map page www.website.com/map, you’ll have to add the attribute post_id to that URL with the real post ID as its value. In our example, your URL should be www.website.com/map/?post_id=15 where 15 is the real post ID.
  2. Edit the file “functions.php” of your theme/child theme and copy/paste the following code in it:
    function cspm_center_map_on_specific_post($default, $map_id){
    	
    	if(isset($_GET['post_id'])){
    		
    		$post_id = esc_attr($_GET['post_id']);
    		
    		$latitude = get_post_meta($post_id, CSPM_LATITUDE_FIELD, true);
    		$longitude = get_post_meta($post_id, CSPM_LONGITUDE_FIELD, true);
    	
    		$output = "
    		setTimeout(function(){
    			cspm_center_map_at_point(plugin_map, map_id, '".$latitude."', '".$longitude."', 'zoom');
    		}, 500);";
    		
    		echo $output;
    	
    	}
    	
    }
    add_filter('cspm_after_map_load', 'cspm_center_map_on_specific_post', 10, 2);

In the same context