1

当我点击GPS-link然后我尝试urla标签中实现。但它不起作用!

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>
...
4

3 回答 3

1

你的 jQuery 代码应该是这样的:

$(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
}

函数应该在.ready()函数之外定义。

于 2013-10-30T11:30:57.567 回答
0

您的showPosition函数需要pos在调用时未收到的参数。

也许,这样的事情会奏效吗?

showPosition(navigator.geolocation.getCurrentPosition());
于 2013-10-30T11:27:19.937 回答
0

首先,这可能是因为您没有正确关闭您的点击功能:

  $("a.test").click(function(){
    if(navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(showPosition);
    } else {
      alert("Geolocation is not supported by this browser.");
    }
  });  
于 2013-10-30T11:31:19.603 回答