Home › Forums › Front-end Issues › Get visitor current location to the map as marker
Hi. I am trying to get visitor locations to the map as marker, to show, where currently visitor is, to compare with another markers etc.. I get his location with this. This works.
if ("geolocation" in navigator) {
// ak je geo, tak spustime lokaciu
navigator.geolocation.getCurrentPosition(function(position) {
// tu si naplnim premenne
var lat = position.coords.latitude,
lng = position.coords.longitude;
console.log(lat, lng);
// nasetujeme hodnoty
$('.currentmarker').attr('data-lat', lat).attr('data-lng', lng);
});
Then in query i loop all markers from post type. As is in documentation. I am trying to solve this issue like this
<div class="marker currentmarker" data-lat="" data-lng="">
<div class="map-popup">
My position
</div>
</div>
<?php $args = array(
'post_type' => 'gastronomie',
//'post_parent' => 22,
'posts_per_page' => 50,
'order' => 'DESC',
'orderby' => 'date',
'lang' => 'de'
//'paged' => $paged
);
query_posts( $args );
$delay=0;
$i=0;
while ( have_posts() ) : the_post();
$location = get_field('location');
?>
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>" data-marker="<?php the_field('pin_color'); ?>">
<div class="map-popup">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</div>
</div>
<?php $delay+=0.2; ?>
<?php endwhile; wp_reset_query(); ?>
As you see, at the top of my query i have also the <div class=”marker”> here i try to get the user longtitude and latitude with .attr. But it not working. I think it is because the map is rendered first and then is render the marker div above the query. So i think the current position of visitor should be implemented to the googlemap JS. But i am not good in jQuery, only copy paste π
here is my googlemap.js
(function($) {
/*
* new_map
*
* This function will render a Google Map onto the selected jQuery element
*
* @type function
* @date 8/11/2013
* @since 4.3.0
*
* @param $el (jQuery element)
* @return n/a
*/
function new_map( $el ) {
// var
var $markers = $el.find('.marker');
// vars
var args = {
zoom : 16,
center : new google.maps.LatLng(0, 0),
mapTypeId : google.maps.MapTypeId.HYBRID
};
// create map
var map = new google.maps.Map( $el[0], args);
// add a markers reference
map.markers = [];
// add markers
$markers.each(function(){
add_marker( $(this), map );
});
// center map
center_map( map );
// add marker cluster
markerCluster( map.markers, map );
// return
return map;
}
/*
* add_marker
*
* This function will add a marker to the selected Google Map
*
* @type function
* @date 8/11/2013
* @since 4.3.0
*
* @param $marker (jQuery element)
* @param map (Google Map object)
* @return n/a
*/
function add_marker( $marker, map ) {
// var
var latlng = new google.maps.LatLng( $marker.attr('data-lat'), $marker.attr('data-lng') );
var defaultMarker = {
path: 'M 0,0 C -2,-20 -10,-22 -10,-30 A 10,10 0 1,1 10,-30 C 10,-22 2,-20 0,0 z M -2,-30 a 2,2 0 1,1 4,0 2,2 0 1,1 -4,0',
fillColor: '#de6b6b',
fillOpacity: 1
};
var blueMarker = {
//path: 'M -1,0 A 1,1 0 0 0 1,0 1,1 0 0 0 -1,0 z',
//path: google.maps.SymbolPath.CIRCLE,
//fillColor: '#42bd2c',
//fillOpacity: 1,
//scale: 3,
//strokeColor: "#42bd2c",
//strokeWeight: 3,
path: 'M 0,0 C -2,-20 -10,-22 -10,-30 A 10,10 0 1,1 10,-30 C 10,-22 2,-20 0,0 z M -2,-30 a 2,2 0 1,1 4,0 2,2 0 1,1 -4,0',
fillColor: '#3292d6',
fillOpacity: 1
}
//Check your marker type, if it's green then set that otherwise leave it grey
var markerType = $marker.attr('data-marker')=="bluemarker"?blueMarker:defaultMarker;
// create marker
var marker = new google.maps.Marker({
position : latlng,
icon : markerType,
map : map
});
// add to array
map.markers.push( marker );
// if marker contains HTML, add it to an infoWindow
if( $marker.html() )
{
// create info window
var infowindow = new google.maps.InfoWindow({
content : $marker.html()
});
// show info window when marker is clicked
google.maps.event.addListener(marker, 'click', function() {
infowindow.open( map, marker );
});
}
}
/*
* center_map
*
* This function will center the map, showing all markers attached to this map
*
* @type function
* @date 8/11/2013
* @since 4.3.0
*
* @param map (Google Map object)
* @return n/a
*/
function center_map( map ) {
// vars
var bounds = new google.maps.LatLngBounds();
// loop through all markers and create bounds
$.each( map.markers, function( i, marker ){
var latlng = new google.maps.LatLng( marker.position.lat(), marker.position.lng() );
bounds.extend( latlng );
});
// only 1 marker?
if( map.markers.length == 1 )
{
// set center of map
map.setCenter( bounds.getCenter() );
map.setZoom( 16 );
}
else
{
// fit to bounds
map.fitBounds( bounds );
}
}
function markerCluster( markers, map ) {
var markerCluster = new MarkerClusterer(map, markers, {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
console.log( markers );
}
/*
* document ready
*
* This function will render each map when the document is ready (page has loaded)
*
* @type function
* @date 8/11/2013
* @since 5.0.0
*
* @param n/a
* @return n/a
*/
// global var
var map = null;
$(document).ready(function(){
$('.acf-map').each(function(){
// create map
map = new_map( $(this) );
});
});
})(jQuery);
Have anyone done this before? Can someone help me please?
Thank you π
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
π€ Curious about the ACF user experience? So are we! Help guide the evolution of ACF by taking part in our first ever Annual Survey and guarantee youβre represented in the results. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 8, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.