[英]How to add a current location marker on Google Map
I have recently developed a website but I haven't been able to show the current location marker on the Google map after the location was found. 我最近开发了一个网站,但是找到位置后,我无法在Google地图上显示当前位置标记。
You can have a look on https://www.storra.com/listings/ 您可以在https://www.storra.com/listings/上查看
I have added a custom listener to the function.php based on Listify auto-locate function but didn't manage to add the marker to the map. 我已经基于Listify自动定位功能向function.php添加了自定义侦听器,但是没有设法将标记添加到地图。 The code is as following,
代码如下
function listify_custom_autolocation() { if ( ! ( is_front_page() || listify_is_job_manager_archive() ) ) { return; } ?> <script> var triggered = false; jQuery(document).on( 'facetwp-loaded', function() { if ( ! triggered ) { var locate = jQuery( '.locate-me' ); locate.trigger( 'click' ); var marker = new google.maps.Marker({ map: map, position: new google.maps.LatLng( parseFloat(coords[0]), parseFloat(coords[1]) ), info: new google.maps.InfoWindow({ content: val.title + val.distance }) }); } triggered = true; }); </script> <?php } add_action( 'wp_footer', 'listify_custom_autolocation', 9999 );
Would very much appreciate if someone could guide me out. 如果有人可以引导我,将不胜感激。 Thank you!
谢谢!
You could use this one for your locate function: 您可以将其用于您的定位功能:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
var marker = new google.maps.Marker({
position: pos,
map: map,
title: 'Your position'
});
map.setCenter(pos);
}, function() {
//handle location error (i.e. if user disallowed location access manually)
});
} else {
// Browser doesn't support Geolocation
}
The following is the relevant code on the backend. 以下是后端的相关代码。 I tried adding the code given by @mxlse but didn't seem to work too.
我尝试添加@mxlse给定的代码,但似乎也无法正常工作。
AutoLocateView.prototype.find = function() { var cv, error, filters, success; cv = this.collectionView; filters = this.filters; if (!navigator.geolocation) { return; } success = function(position) { var lat, lng; lat = position.coords.latitude; lng = position.coords.longitude; cv.set({ 'lat': lat, 'lng': lng }); return $('.locate-me').removeClass('loading'); }; error = function() { $('.locate-me').removeClass('loading'); return filters.startup(); }; navigator.geolocation.getCurrentPosition(success, error); return this; };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.