地理定位Google Maps链接jQuery [英] Geolocation Google Maps Links jQuery
问题描述
当我点击移动电话上的链接时,我想提示用户是否要允许检测到该位置,如果是,请打开Google地图,并从当前位置开始到达最终目的地。
这就是我所拥有的,但是当我点击链接时,它会进入后备默认链接。
< a class =geohref =http://maps.google.com/maps?ll=40.822419,-73.993124>获取路线< ; / A>点击(函数(e)){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(getLoc,handle_errors)
$ b $(a.geo ;
}
e.preventDefault();
});
函数getLoc(位置){
var geoLat = position.coords.latitude;
var geoLon = position.coords.longitude;
var gmapLink =http://maps.google.com/maps?saddr=+ geoLat +,+ geoLon +& daddr = 40.822419,-73.993124;
$(a.geo)。attr(href,gmapLink);
$ / code>
如何将新链接替换为用户位置后的href用户点击链接?
替换你的行,改变 href
属性:
$(a.geo)。attr(href,gmapLink);
重定向:
window.location.assign(gmapLink);
移动 e.preventDefault()$也是值得的c $ c>> if(navigator.geolocation)
块中,这样如果没有可用的地理位置,用户仍然会前往Google地图并自行插入其位置。
When I click on a link on the mobile phone, I would like a prompt to ask the user if they would like to allow the location to be detected, and if yes, open Google Maps with the directions starting from their current location to the end destination.
This is what I have, but when I click on the link, it goes to the fallback default link.
<a class="geo" href="http://maps.google.com/maps?ll=40.822419,-73.993124">Get Directions</a>
$("a.geo").click(function(e)) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(getLoc, handle_errors);
}
e.preventDefault();
});
function getLoc(position) {
var geoLat = position.coords.latitude;
var geoLon = position.coords.longitude;
var gmapLink = "http://maps.google.com/maps?saddr=" + geoLat + "," + geoLon + "&daddr=40.822419,-73.993124";
$("a.geo").attr("href", gmapLink);
}
How do I replace the href with the new link with the user's location AFTER the user clicks on the link?
Replace your line which changes the href
attribute:
$("a.geo").attr("href", gmapLink);
with a redirect:
window.location.assign(gmapLink);
It would also be worth moving the e.preventDefault()
inside the if (navigator.geolocation)
block, so that if there is no geolocation available, the user still goes to google maps and can insert their location themselves.
这篇关于地理定位Google Maps链接jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!