Skip Navigation

[Resolved] Create Link on CPT Content Template That Links to Map Marker Focused

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user would like to create links that will focus on their related marker on a view's map.

Solution:
There is no such feature. But it can be implemented using custom Javascript code. Check this solution https://toolset.com/forums/topic/create-link-on-cpt-content-template-that-links-to-map-marker-focused/#post-2119783

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/maps/maps-shortcodes/#map-focus-on-marker

This support ticket is created 3 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 4 replies, has 2 voices.

Last updated by Clayton Chase 3 years, 6 months ago.

Assisted by: Jamal.

Author
Posts
#2116831

Hey guys,

I'm wondering if it is possible to create a link on a content template of a CPT that links back to the page the map is on and focuses in on the address stored for that post? I hope that makes sense. I've looked through documentation and other tickets but can't find a working solution.

Basically I'm storing the address in a field on a custom post type and I'd like to then create a link that focuses on that point on my Toolset Map on a page where I've created my view and map.

#2117241

Hello Clayton and thank you for contacting Toolset support.

The "Focus on Marker" feature is implemented using Javascript, so, it can't work across pages. At least, not without custom code. You can have this feature on the same page. For example, the page where the view and the map are can have links for posts that will focus on each post's marker on the map. But from a different page, that can't be done out of the box.

You may, as a workaround, build a special link(for example, that has a URL parameter ?focus-on=1234, where 1234 is the post ID) and have a special Javascript code that will look for the URL parameter "focus-on" and programmatically trigger a click on a hidden "Focus on Marker" button the view's page. Does it make sense?

I hope this answers your question. Let me know if you have any questions.

#2119233

Hey Jamal,

Thanks for explaining how it works a bit. Would you be able to include a code snippet to get me started?

#2119783

First of all the map and view should be created with the legacy editor. We have currently a bug that prevents the focus-on-marker feature to work on blocks-based views.

Basically, you will add a URL parameter to the post's URL, for example(?focus-on=[wpv-post-id]):

<a href="<em><u>hidden link</u></em>;">View on the map</a>

Then, on the target page, you will add a Focus on marker link or button, and a marker. The marker_id should be the same as data-marker. And you must use the correct map id. Check this documentation https://toolset.com/documentation/programmer-reference/maps/maps-shortcodes/#map-focus-on-marker

<button class="js-wpv-addon-maps-focus-map" data-map="map-6" data-marker="[wpv-post-id]-marker-id">Focus on marker</button>
[wpv-map-marker map_id='map-6' marker_id='marker-[wpv-post-id]' marker_field='wpcf-location'][/wpv-map-marker]

And then add the Javascript code that will check the URL parameter and trigger the click event if necessary, the code should wait for the map initialization to complete, and set a timeout to let the API put the marker on the maps. Something like:

function urlParam(name){
    var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
    return results[1] || 0;
}
jQuery(function($){
  $( document ).on( 'js_event_wpv_addon_maps_init_map_completed', function( event, data ) {
    // console.log('map init completed')
    setTimeout(function(){
      var focusOn = urlParam('focus-on')
      // console.log('setTimeout and focus on', focusOn)
      jQuery('button[data-marker="marker-' + focusOn +'"]').click()
    }, 3000)
  });
})

Please note that custom code is out of the scope of the support forum. If you are not comfortable with programming, consider hiring a developer. Here is a list of our partners https://toolset.com/contractors/

#2119941

I'm using the block view builder so this solution won't work anyway. I appreciate your help though.