Render map with short code google maps

Hi i am currently using the google maps and am using the Custom field feature which works great when i click automatically show map. My problem is i want to load the map elsewhere other than under or above the post. I have a tab that i wanted loaded in and im trying to get it to render but it just comes out blank

<?php echo do_shortcode('[map id="ecpt_address"]'); ?>

what is the shortcode i need to add to make the map render the address custom field when the post has one?

thanks!

    Kimberly

    Peter,

    There are a few details on using the Google Maps shortcodes here

    https://premium.wpmudev.org/project/wordpress-google-maps-plugin/#usage

    have you seen these? It should display fine if you are using those

    have you tested the non-php shortcode in a post or page to see if it's displaying correctly? If so then we know it's probably just something with the code context

    Best,

    Kimberly

    Peter76

    yeha it works perfectly right out of the box...i see the map now but its not picking up my address when i try to add it to my custom post php it doesnt pick up my custom field for the address.

    i need it to query my address custom field to get the address.it works perfectly when i activate it to display from the backend but when i try to insert it into my theme i dont know how to get it to query the custom field

    none of the shortcode query examples show this as an example but its the out of the box example if you have "Automatically show the map" to yes it shows with the marker perfectly......

    Ive tried all the query short codes but no luck...ive found

    query="all"
    query="tag=my_tag"
    query="post_type=my_post_type"

    but i need one for address_field or custom_field but theres no documentation for what we can call.......sorry for being confusing!

    Kimberly

    Peter,

    Here is some advice from the developer about someone wanting to do what you are doing but with Zipcodes. Perhaps this will be useful. I would paraphrase, but it needs context

    Actually, you should be able to do what you need with query shortcode attribute, but in a slightly different way, like this [map query="meta_value=ZIP_CODE"], where ZIP_CODE is the zip code you wish to search for. Behind the scenes, this query will gather all the posts that have their wp_postmeta.meta_value field set to ZIP_CODE, and then check for any associated maps. The query attribute currently won't allow multiple stacked queries, but the zip should be relatively unique and consistent, at least enough to ensure the proper results.

    Here is the link if you're up for some reading
    https://premium.wpmudev.org/forums/topic/search-by-custom-field

    Peter76

    really interesting discussion but my problem is a little bit easier!
    Take A look at the screen shot of my settings page
    When i click "Automatically show map" it works perfectly. the only problem is i dont want it either above or below the content ....i want to place it elsewhere.

    I am trying to just replicate what it is doing automatically but position it in my custom template file rather than below or above post

    i tried query="meta_value=address" and ecpt_address etc etc but nothing works

    Emanaku

    Hi Peter76,

    apparently the plugin does not allow to do what you want because there is no parameter for the shortcode telling it it should use the address field.

    The clean way would be to extend the plugin's interface by sth like you tried e.g.
    [map usage="address_field"] or similar.

    Also the functions of the plugin are not easy to use outside the plugin because they are kind of capsuled.

    So: How desperate are you?

    Here is a way you could have just the map, but it takes three steps to go there:

    1. In the settings you have to turn on "Automatically show map - YES" and "... the new map will be shown "ABOVE" the post body". I know you do not want that - that's why there is step 2:

    2. Because you do not want to show the automatic map you define a filter removing the map from the post. You add these lines in functions.php of your theme:

    function ema_remove_map($text) {
    	return preg_replace('/\[map .*?\]/','',$text);
    }
    add_filter('the_content','ema_remove_map');

    Caveat: if you ever somewhere else are using the map shortcode then this filter will remove it, too.

    3. To get the map alone from a post with address field you add the following lines in the page or post template where you want to show the map (it has to be inside the loop):

    function ema_find_map($text){
           $pattern = '/[ \]]/';
    	$result = preg_split($pattern, $text);
              // assumption: option in Google Maps &quot;Above&quot; post
    	if(isset($result[0]) && $result[0]=='[map')
    		return '[map '.$result[1].']';
    	return '';
    }
    remove_filter('the_content','ema_remove_map');
    add_filter('the_content', 'ema_find_map', 2);
    $temp_content = apply_filters('the_content',get_the_content());
    echo $temp_content;
    remove_filter('the_content', 'ema_find_map', 2);
    add_filter('the_content','ema_remove_map');

    These lines get the content of the current post/page and replace it with the map shortcode (only) AFTER the plugin has created it, but before the plugin replaces the shortcode by the map (that is why the priority of the filter is set to 2). This way the result of $temp_content is just the map itself, and you can echo it whereever you want on the post/page.

    The attachment shows an example post with the map above all - you can put it whereever you want on the post's page.

    Thank you for this challenging question ...

    Have fun!!

    Kimberly

    Hi!

    Just checking in

    I'm guessing that because we haven't heard back from you that you've got your issue worked out or that we have reached the end of our convo here. If you are still having problems or need any other answers on this particular topic please feel free to come back to this thread at anytime. Just be sure to mark it Not Resolved so we don't miss it!

    This ticket is only being Resolved due to lack of activity. It is by no means an action to avoid, brush off or minimize your issue. We are ready and here if you want to re-open at any time and I apologize in advance for any lack of communication if this ticket is being closed in error.

    Thanks for using the WPMU Community!

    Best,
    Kimberly

    Peter76

    shoot guys i thought i answered and i started other aspects of the job ! Sorry!!. I couldnt get Emanaku's awsome help to work but i really keep thinking that this is easier than im making it...i can implement this with other plugins the problem is i got a grey google maps issue becasue im displaying it in a tab. But with the wpmu plugin i dont have the grey map problem ..

    what ive done with other plugins ive tried to adapt to the wpmu dev one becasue i dont have they greyed out maps

    <?php echo do_shortcode('[map query="' . get_post_meta(get_the_ID(), 'ecpt_address', true) . '" show_map="true" overlay="true" width="100%"]'); ?>

    this jsut shows a map of new york with no markers (i dont want to show it new york)
    i guess i cant query an address .....if i cant do this easily i gotta find a solution for the grey maps problem with other plugins.....i have a couple hundred properties to add maps to so i really dont feel like creating them manually one by one!

    Emanaku

    Hi Peter,

    it seems you got into the (vicious) circle of your opening post again The map query shortcode does not accept an address. And it does not accept a reference to the meta field of a post with an address. The only way to get the plugin to create the map from the address is to create the content of the post - as described before ... that is bitter, but it is like it is for now.

    So instead of experimenting with these approaches which do not work you can use my functions above and even create a new function for all this filtering and unfiltering ...:

    function ema_print_map() {
    	remove_filter('the_content','ema_remove_map');
    	add_filter('the_content', 'ema_find_map', 2);
    	$temp_content = apply_filters('the_content',get_the_content());
    	echo $temp_content; // prints map
    	remove_filter('the_content', 'ema_find_map', 2);
    	add_filter('the_content','ema_remove_map');
    }

    put it into the functions.php (together with the other two above.
    Then: within the loop on a page or post template you can just call this function and it will produce the map - that is the easy part.

    Your other problem "Map not showing when created in a hidden tab and then the user will see the map":

    That apparently is a well known problem - a little bit of googling brings up many references to pages where people have the same problem.

    The basic solution seems to be the one which is proposed here (but also on other pages): http://stackoverflow.com/questions/3782632/google-map-display-from-a-hidden-area

    It says that you have to call the javascript function
    google.maps.event.trigger(map, 'resize')
    as soon as you make the HTML element (mostly a div) with the map visible. (e.g. you bind this function call to an "onClick" event of the tab).

    Sounds easy enough, doesn't it?

    Unfortunately the variable "map" which is used in all the google map documentation examples is not set with the WPMUDEV Google Maps plugin.

    Instead of the variable for the map is hidden somewhere in the depths of the code where I cannot find it ... I inspected the whole environment of the page with firebug and other tools - and it looks I am not skilled enough to find out what is the corresponding map-variable in the plugin which could be triggered by a resize ...

    So: Either the developer Ve tells us this information and if this is possible at all ... or we are lost for now ... sorry!

    Emanáku

    Peter76

    Emanaku! ok sorry for reopening the topic. i got the map working

    but now im getting
    Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'ema_remove_map' was given in /home/content/46/9117646/html/local/wp-includes/plugin.php on line 170

    the error is displaying on the first tab and wipes out its content but when i click the map tab it works.

    im calling the function like this <?php ema_print_map();?>

    the map mark is not centered though, it shows at the top left of the page. i tried adding the addon to center the map but nothing!

    Emanaku

    Hi Peter, I am sorry for my last post - if I could I would delete it: I am using another plugin, too (WP Geo) and there you can say if you want to center the map per post.

    The Google Maps Plugin has a lot of options per post (when you click the Google Maps symbol at the visual editor and then "Edit" and then "Map Options"), but none of them affects the centering of the map.

    At least you can see the preview of the map here and check if the map is shown the way you want it to be shown ...

    Sorry again for the former misinformation ...