Skip Navigation

[Resolved] Draw a circle on CRED Google Map

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

Problem:
How to draw a circle on map with custom radius using toolset map

Solution:
You can use Toolset Maps javascript event hook "js_event_wpv_addon_maps_init_map_completed" to draw a circle on google map.

You can find proposed solution, in this case, with the following reply:
=> https://toolset.com/forums/topic/draw-a-circle-on-cred-google-map/#post-690616

Relevant Documentation:

This support ticket is created 6 years, 8 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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: Asia/Kolkata (GMT+05:30)

Tagged: 

This topic contains 11 replies, has 3 voices.

Last updated by adrianM-6 6 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#671866

Tell us what you are trying to do? I am trying to draw a circle on a CRED Google Map on the following page:

hidden link

I have a 'location field', which is the Google Map field, and another field called 'coverage-distance'. I would like to draw a circle with a radius of 'coverage-distance' on the location map, once this has been inputted. This is what I've tried:

jQuery( function( $ ) {
  
$('input[name=coverage-distance]').change(draw-circle);

function draw-circle() {
 
 var coverageCircle = new google.maps.Circle({
      strokeColor: '#FF0000',
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: '#FF0000',
      fillOpacity: 0.35,
      map: toolset-google-map-preview,
      center: $('input[name=location]').val(),
      radius: $('input[name=coverage-distance]').val(),
});
}
});

I'm pretty sure I haven't referenced the map properly, but can you see any other problems?

#673237

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - Toolset maps offers event 'js_event_wpv_addon_maps_init_map_completed' which you may use in this case to set the map options.

For example:

jQuery(document).ready(function($){

$( document ).on('js_event_wpv_addon_maps_init_map_completed', function( event, event_settings ) {
    // Get the map using our .get_map() method...
    var map = WPViews.view_addon_maps.get_map( event_settings.map_id );

    // And then use Maps API .setOptions() method to programmatically change any option on the map.
    if ( map ) {
        map.setOptions({strokeColor: ''#FF0000'});
    }
} );

});

This is the only way you can add/set options for Toolset maps.

#677611

Hi Minesh

Thanks, but when does this event happen? Is it on page load, or after the map has been loaded?

I want it to use the value of another field in the same form. Does that mean I need to get the user to enter the radius field first, before the map location field?

#677816

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - as you can see - I've added the hook "js_event_wpv_addon_maps_init_map_completed" within the document ready event (page load) event so - yes, so you need to input the value of radius first before map gets load.

#683001

Hi Minesh

Thanks, but I'm still not really sure how to create the circle object?

This is what I've tried:

jQuery(document).ready(function($){
 
$( document ).on('js_event_wpv_addon_maps_init_map_completed', function( event, event_settings ) {
    // Get the map using our .get_map() method...
    var myMap = WPViews.view_addon_maps.get_map( event_settings.map_id );
 
    // And then use Maps API .setOptions() method to programmatically change any option on the map.
    if ( myMap ) {
      var coverageCircle = new google.maps.Circle({
      strokeColor: '#FF0000',
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: '#FF0000',
	  map: myMap,
      fillOpacity: 0.35,
      center: $('input[name=location]').val(),
      radius: $('input[name=coverage-distance]').val(),
    });
   
    }
} );
 
#690214

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

No you should try to use following method.

jQuery(document).ready(function($){
 
$( document ).on('js_event_wpv_addon_maps_init_map_completed', function( event, event_settings ) {
    // Get the map using our .get_map() method...
    var map = WPViews.view_addon_maps.get_map( event_settings.map_id );

 var options = { strokeOpacity: 0.8,
                           strokeWeight: 2,
                           fillColor: '#FF0000',
                           fillOpacity: 0.35,
                          center: $('input[name=location]').val(),
                          radius: $('input[name=coverage-distance]').val()
                        };
 
    // And then use Maps API .setOptions() method to programmatically change any option on the map.
    if ( map ) {
        map.setOptions(options);
    }
    });
});
#690513

Hi Minesh

Thanks but I'm still not seeing a circle.

But surely we need to create the new circle object before setting the options?

Aren't we trying to set those options for the circle, not for the map?

#690616

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I just tested on my localhost with static center and radius value and I see its working. See the following image:
=> hidden link

I've used following code:

jQuery(document).ready(function($){
  
$( document ).on('js_event_wpv_addon_maps_init_map_completed', function( event, event_settings ) {
    // Get the map using our .get_map() method...
    var myMap = WPViews.view_addon_maps.get_map('map-11');
   
  // center value 
   var center = new google.maps.LatLng(21.7644725, 72.1519304);     


    // And then use Maps API .setOptions() method to programmatically change any option on the map.
     var coverageCircle = new google.maps.Circle({
      strokeColor: '#FF0000',
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: '#FF0000',
      fillOpacity: 0.35,
       map: myMap, 
      center: center,
      radius: 100,
    });
    
   
} );
  
  } );
#690646

Hi Minesh

I've tried this with some minor changes:

jQuery(document).ready(function($){
   
$( document ).on('js_event_wpv_addon_maps_init_map_completed', function( event, event_settings ) {
    // Get the map using our .get_map() method...
    var myMap = WPViews.view_addon_maps.get_map(event_settings.map_id);
   var myCenter = new google.maps.LatLng(-30.061807, 30.877525);   
 
    // And then use Maps API .setOptions() method to programmatically change any option on the map.
     var coverageCircle = new google.maps.Circle({
      strokeColor: '#FF0000',
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: '#FF0000',
      fillOpacity: 0.35,
       map: myMap, 
      center: myCenter,
      radius: 100,
    });
     
    
} );
   
  } );

I still don't see a circle. I've just changed your map id to event_settings.map_id. How can I find the map id to manually specify it?

I've also changed the lat long values to an address that I am inputting to create the map.

And I changed the center variable to myCenter. Apart from that my code seems to be the same as yours. But no circle?

#690718

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - the solution I shared should work when you display map as given under where you can see the map ID:

[wpv-map-render map_id="map-11"][/wpv-map-render]
[wpv-map-marker map_id="map-11" marker_id="marker-11" marker_field="wpcf-student-address"][/wpv-map-marker]

There is no such hook or filter available that you can add circle while adding the address. This needs custom programming which is beyond the scope of our support policy.

If you need custom programming for your project, please feel free to contact our certified partners at following link:
=> https://toolset.com/toolset-support-policy/

#723498

OK Cool Thanks Minesh

#1239143

It works like this:

jQuery(document).on('js_event_wpv_addon_maps_init_map_completed', function (event, event_settings) {
    let map = WPViews.view_addon_maps.get_map(event_settings.map_id);
    google.maps.event.addListenerOnce(map, 'idle', function () {
        let coverageCircle = new google.maps.Circle({
            strokeColor: '#ed1c24',
            strokeOpacity: 0.7,
            strokeWeight: 1,
            fillColor: '#ed1c24',
            fillOpacity: 0.15,
            map: map,
            center: map.center,
            radius: 2000,
        });
    });
});

With help of https://stackoverflow.com/a/7262773/1165132