Tell us what you are trying to do?
I'd like to add a marker to a map that is specified by the user NOT their geocode position, but an address that they enter.
Is there any documentation that you are following?
No
Is there a similar example that we can see?
I am trying to emulate this hidden link - the results shown include a marker for the address the user entered rather than the location they are according to geocode.
There's nothing built-in with Toolset that will let you add such a marker with the search location, but it should be possible with some code.
I'm most of the way there, so that the marker for the location is added when a search is submitted, but my problem is getting the map to redraw, because the marker may be added outside the bounds of the existing map and not be visible.
I'm consulting with the devs about getting that working, their initial pointers didn't work.
Leave it with me and I'll get back to you when I have a solution.
It's not clear that our maps implementation includes an internal method to achieve this, and instead I've gone with using the Google Maps API directly to update the boundaries of the map to include the added marker for the search origin.
The marker is added via JavaScript on the front-end, but that code is assembled using PHP on the server so that some of the required data for coordinates etc. can be incorporated.
You'll need to add the following PHP, and the only thing you should need to change is the ID of the map ("map-12" in the same below):
Sorry, I see an error on your site using this, and spot a problem where I had made a slight change on my test site but had the prior code in my clipboard before pasting it in my reply above.
You need to remove the line that has icon: image, so the complete code for the entire snippet (including the required code at the top) would be this:
<?php
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
/**
* Add a marker for the search location to a map
*/
add_action( 'init', 'ts_distance_center_coordinates', 101 );
function ts_distance_center_coordinates(){
if ( isset($_REQUEST['toolset_maps_distance_center']) )
{
$mapID = 'map-12'; // Edit
$input = $_REQUEST['toolset_maps_distance_center'];
$coords_array = Toolset_Addon_Maps_Common::get_coordinates( $input );
$lat = $coords_array['lat'];
$lon = $coords_array['lon'];
$script = "
( function( $ ) {
$( document ).bind( 'js_event_wpv_addon_maps_init_map_completed', function(){
const mapid = '$mapID';
const theMap = WPViews.view_addon_maps.get_map(mapid);
const theLatLng = { lat: $lat, lng: $lon };
const searchOrigin = new google.maps.Marker({
position: theLatLng,
theMap,
title: 'Search origin',
});
searchOrigin.setMap(theMap);
// update bounds once map drawn
google.maps.event.addListenerOnce(theMap, 'idle', function() {
const bounds = theMap.getBounds();
bounds.extend(theLatLng);
theMap.fitBounds(bounds);
});
});
})( jQuery );
";
// add inline script to front end
wp_register_script( 'ts_distance_center_coords', '', array('jquery'), '', true );
wp_enqueue_script( 'ts_distance_center_coords' );
wp_add_inline_script( 'ts_distance_center_coords', $script);
}
}
Yes, you can customise the marker as described in the Google API docs, same link I shared before: hidden link
In the updated code below I use one of the alternative icons provided by the Toolset Maps plugin itself, but you could provide an alternative yourself. I also added an animation where the pin is dropped onto the map. Also, a tooltip is already included (the title attribute currently set to "Search origin").
The Google API is pretty flexible and you can make changes to the marker as required by updating options in the added code.
Here's the updated version:
<?php
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
/**
* Add a marker for the search location to a map
*/
add_action( 'init', 'ts_distance_center_coordinates', 101 );
function ts_distance_center_coordinates(){
if ( isset($_REQUEST['toolset_maps_distance_center']) )
{
$mapID = 'map-12'; // Edit
$input = $_REQUEST['toolset_maps_distance_center'];
$coords_array = Toolset_Addon_Maps_Common::get_coordinates( $input );
$lat = $coords_array['lat'];
$lon = $coords_array['lon'];
$script = "
( function( $ ) {
$( document ).bind( 'js_event_wpv_addon_maps_init_map_completed', function(){
const mapid = '$mapID';
const theMap = WPViews.view_addon_maps.get_map(mapid);
const theLatLng = { lat: $lat, lng: $lon };
const searchIcon = '<em><u>hidden link</u></em>';
const searchOrigin = new google.maps.Marker({
position: theLatLng,
theMap,
icon: searchIcon,
animation: google.maps.Animation.DROP,
title: 'Search origin',
});
searchOrigin.setMap(theMap);
// update bounds once map drawn
google.maps.event.addListenerOnce(theMap, 'idle', function() {
const bounds = theMap.getBounds();
bounds.extend(theLatLng);
theMap.fitBounds(bounds);
});
});
})( jQuery );
";
// add inline script to front end
wp_register_script( 'ts_distance_center_coords', '', array('jquery'), '', true );
wp_enqueue_script( 'ts_distance_center_coords' );
wp_add_inline_script( 'ts_distance_center_coords', $script);
}
}