When I click on the GPS-link
then I try implementing the url
in the a
tag.
But it does not work!
jQuery:
$(document).ready(function(){
/* add geolocation */
$("a.target").click(function(){
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
else
{
alert("Geolocation is not supported by this browser.");
}
}
function showPosition(pos){
var lat = pos.coords.latitude;
var lon = pos.coords.longitude;
/* GPS link */
$("a.target").attr("href",
"http://dev.market-locator.com/geolocation-lijst?distance[latitude]="
+ lat + "&distance[longitude]=" + lon
+ "&distance[search_distance]=20&distance[search_units]=km");
}
});
HTML:
...
<a class="target">Link</a>
...
Your jQuery code should be this:
$(document).ready(function(){
/* add geolocation */
$("a.target").click(function(){
if(navigator.geolocation) {
showPosition(navigator.geolocation.getCurrentPosition());
}
else
{
alert("Geolocation is not supported by this browser.");
}
});
});
function showPosition(pos){
var lat = pos.coords.latitude;
var lon = pos.coords.longitude;
/* GPS link */
$("a.target").attr("href", "http://dev.market-locator.com/geolocation-lijst?distance[latitude]=" + lat + "&distance[longitude]=" + lon + "&distance[search_distance]=20&distance[search_units]=km");
window.location.href = "http://dev.market-locator.com/geolocation-lijst?distance[latitude]=" + lat + "&distance[longitude]=" + lon + "&distance[search_distance]=20&distance[search_units]=km"; //add this line
}
Function should be define outside of the the .ready()
function.