Home › Toolset Professional Support › [Resolved] Focus on map user position
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)
This topic contains 15 replies, has 2 voices.
Last updated by umbertoZ 2 days, 21 hours ago.
Assisted by: Minesh.
Hi, I’m having some issues with Toolset Maps (using OpenStreetMap).
1 – Focus on user position
I’m using the maps-focus-map button to show the user’s current position. Unfortunately, it doesn’t work correctly: the marker is placed in the middle of nowhere (see shot1.jpg).
Here’s my code:
[wpv-map-render map_id="map-20" map_height="300px" single_zoom="16" map_type="satellite" map_type_control="off" street_view_control="off"][/wpv-map-render] [wpv-map-marker map_id='map-20' marker_id='marker-user-focus' marker_title='Tu posición' marker_icon='<em><u>hidden link</u></em>' map_render='wait' current_visitor_location='true']Tu posición[/wpv-map-marker] <button class="js-wpv-addon-maps-focus-map" data-map="map-20" data-marker="marker-user-focus">Centrar en mi ubicación actual</button>
On the same page, I also have a View that loads a list of markers on the same map.
You can see the issue on this page:
hidden link
hidden link
2 – Address field zoom
On this page I also have a form where the user can save either their current location or any other address in an Address field on the map. This part works, but when a position is selected, the map doesn’t zoom in to that location (see form1.jpg).
On mobile especially, this is not very user-friendly, since the user has to manually zoom in to check and refine their selection.
Is there a way to make the map automatically zoom to the selected position when it reloads?
You can see the issue on this page:
hidden link
This is the form:
hidden link
Thanks!
Hello. Thank you for contacting the Toolset support.
Can you please check now:
- hidden link
I've added the code snippet namely "get-current-user-location" to "Custom Code" section offered by Toolset and added the following code to it.
=> hidden link
function get_user_location_by_ip() { // Get user IP address $ip = $_SERVER['REMOTE_ADDR']; // Optional: handle proxies/load balancers if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ipList = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); $ip = trim($ipList[0]); } // API endpoint (no key needed) $url = "<em><u>hidden link</u></em>{$ip}?fields=status,message,country,regionName,city,lat,lon,query"; // Initialize cURL $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 5); // Execute and decode response $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode !== 200 || !$response) { return null; } $data = json_decode($response, true); if (isset($data['status']) && $data['status'] === 'success') { return array( 'ip' => $data['query'], 'country' => $data['country'], 'region' => $data['regionName'], 'city' => $data['city'], 'latitude' => $data['lat'], 'longitude' => $data['lon'], ); } return null; } function func_get_location_by_ip_shortcode($atts) { // Default attribute $atts = shortcode_atts(array( 'attribute' => '' ), $atts, 'get-location'); $location = get_user_location_by_ip(); if (empty($location)) return ''; $attribute = strtolower($atts['attribute']); switch ($attribute) { case 'lat': return esc_html($location['latitude']); case 'lon': return esc_html($location['longitude']); case 'city': return esc_html($location['city']); case 'region': return esc_html($location['region']); case 'country': return esc_html($location['country']); case 'ip': return esc_html($ip); default: return ''; // Unknown attribute } } add_shortcode('get-location', 'func_get_location_by_ip_shortcode');
We are using function get_user_location_by_ip() to get the current user location based on the current user IP and use the custom shortcode [get-location] to get the lat and lon associagted to current user location address.
On your page - I've modified the marker code as given under:
[wpv-map-marker map_id='map-20' marker_id='marker-user-focus' marker_title='Tu posición' lat='[get-location attribute="lat"]' lon='[get-location attribute="lon"]' marker_icon='//stopvertidosilegales.org/wp-content/uploads/user-position.png' ]Tu posición[/wpv-map-marker]
Can you please confirm it works as expected.
Hi Minesh, thank you for the quick response.
I tested on my laptop, and the marker is placed about 600 km away from my actual location. This might be related to my current IP.
I also tested on my mobile phone with GPS and location permissions enabled in Chrome, and the marker is still placed in the same incorrect location.
Additionally, I reset the location permission in Chrome (as a test), but now the website no longer prompts me to grant permission to use the GPS location.
Can you check it again?
thanks
Can you please check now.
I've adjusted the code added to the page as given under:
=>hidden link
[wpv-geolocation message_when_missing="We need your location to show this content"] [wpv-map-render map_id="map-20" map_height="300px" single_zoom="16" map_type="satellite" map_type_control="off" street_view_control="off"][/wpv-map-render] [wpv-map-marker map_id='map-20' marker_id='marker-user-focus' marker_title='Tu posición' lat="[get-location attribute='lat']" lon="[get-location attribute='lon']" marker_icon='//stopvertidosilegales.org/wp-content/uploads/user-position.png' ]Tu posición[/wpv-map-marker] [/wpv-geolocation]
I've also adjsuted the code snippet namely "get-current-user-location" that is added to "Custom Code" section offered by Toolset as given under.
=> hidden link
function func_get_location_by_ip_shortcode($atts) { // Default attribute $atts = shortcode_atts(array( 'attribute' => '' ), $atts, 'get-location'); if(isset($_COOKIE['toolset_maps_location']) and $_COOKIE['toolset_maps_location']!=''){ $location = explode(",",$_COOKIE['toolset_maps_location']); } if (empty($location)) return ''; $attribute = strtolower($atts['attribute']); switch ($attribute) { case 'lat': return esc_html($location[0]); case 'lon': return esc_html($location[1]); default: return ''; // Unknown attribute } } add_shortcode('get-location', 'func_get_location_by_ip_shortcode');
Can you please confirm it works as expected.
Hi Minesh, It works fine on my laptop, but not on my mobile. Instead of the map, it shows the message “We need your location to show this content” (see screenshot screen0).
Also, it doesn’t automatically prompt for location permissions. The permission request only appears when I click “Use my location” in the form map.
In addition, I sometimes run into issues with the CSS not loading properly (see screenshots screen1 and screen2).
Cheers,
Well - you have to compromize a bit here as you are using the OpenStreet Map and we are at early stage of open street map.
You need to add a button so user can share their location and hide the content using the if user did not share the location. I do not have any other solution/workaround, I'm afraid.
Hi, the problem on mobile is that even after I grant permissions to use my location, it still display th emessage “We need your location to show this content”.
cheers
I checked with my android mobile and I can see its working on mobile as well.
I suggest you clear cache on your mobile and then try to use private mode and try to access the page:
- hidden link
When you try to access the above page, it asks for login, I logged-in using the access details you shared and after login it asks me to share the location and as soon as I share the location I can see the map is loaded without any issue with the current location.
Hi Minesh, thanks for the adivice, it works rasonably well.
I've got another message from you with this text:
Can you please check now: xxxxxxx
There seems to be issue with one of the widgets added and now I removed all the widgets and I can see it does not jump much.
Maybe you want to try to use any of the recomonded theme from Toolset:
- https://toolset.com/documentation/recommended-themes/
Maybe it's a mistake, I also don't see it in this threat.
In my first message I asked also for a look to the zoom issue on the form map. Can you tell me if there is a solution to zoom to the selected marker after the user write an address?
thanks
That message was cross postsed and not for you.
In my first message I asked also for a look to the zoom issue on the form map. Can you tell me if there is a solution to zoom to the selected marker after the user write an address?
===>
If I'm not wrong you are talking about the step #3 "Marca la posición en el mapa", under that I can see the address field, if you enter some address I can see it offers related address with auto-complete and as soon as I select one of the address I can see that selected address marker on the map given under the address text box.
Yes, you are right, it's the step 3 and everything works fine, but the marker is displayed on the map with a very low zoom, so the user has to click plus button many times to zoom in the position and refine it if necessary. Is it possible to zoom it closer to the marker position after address selection?
thanks
I've added the following custom JS code to custom JS section of your Toolset form:
=> hidden link
var delayed = function() { jQuery(".js-toolset-maps-address-autocomplete").each(function(index,item){ previewmap = WPViews.mapsAddressAutocomplete.maps[0]; currentzoom = previewmap.getZoom(); previewmap.setZoom(7); }); }; jQuery(document).ready(function(){ setTimeout( delayed, 2000 ); });
I've set the zoom level to 7, you can adjust the value as required for your map address field added on the form.
I do not see that issue. Can you please share exact steps I will requie to follow in order to see the issue.
Hi, I don't know exactly the steps. It happens sometimes, usually on the first load of any page or after login.
cheers