简体   繁体   English

HTML5地理位置

[英]HTML5 Geolocation

I use this to geolocate the user and I need the values of currentLat and currentLon stored in a var: 我用它来定位用户,我需要存储在var中的currentLatcurrentLon的值:

    <script>  

  window.onload = function() {
    var startPos;

    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position) {
        startPos = position;
        document.getElementById("currentLat").innerHTML = startPos.coords.latitude;
        document.getElementById("currentLon").innerHTML = startPos.coords.longitude;
      });
    }
  };

</script>

I need those values in here: 我在这里需要这些值:

http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=my_key&radius_units=km&radius=5&lat= XXX &lon= XXX &jsoncallback=?

How do I get the values through jQuery and put them in the URL immediately? 如何通过jQuery获取值并将其立即放在URL中? What's the quickest way? 最快的方法是什么?


Thanks @Manuel but I have used it like this first as I don't need the error function, just to keep it simple so I can understand it but doesn't work?! 感谢@Manuel,但是我首先使用了它,因为我不需要错误功能,只是为了使其简单起见,以便我能理解它,但是不起作用?

$(document).ready(function() {
  navigator.geolocation.getCurrentPosition(function(position) {
    var lat = position.coords.latitude;
    var lon = position.coords.longitude;
  var JSONURL = "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=my_key&format=json&privacy_filter=1&media=photos&tag=london&minwidth=700&has_geo=1&accuracy=11&content_type=1&extras=geo,owner_name,url_m&page=1&radius_units=km&radius=5&"+lat+"&lon="+lon+"&jsoncallback=?";
  jQuery.getJSON( JSONURL, getJSONimages);
  function getJSONimages(data) {
    var htmlString = "";
    $.each(data.photos.photo, function(i,item){
    var itemTitle = item.title;
                }); 
    $('#slideshow').html(htmlString);
    $('#slideshow').slideshow({
                timeout: 3000,
                type: 'random',
                fadetime: 2000
            });   
  } }
  })   

in the var url you'll get find the url with the lat an lon filled in var url您将找到包含lon的网址

navigator.geolocation.getCurrentPosition(function(position) {
    var lat = position.coords.latitude;
    var lon = position.coords.longitude;

    var url = 'http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=my_key&radius_units=km&radius=5&lat='+lat+'&lon='+lon+'&jsoncallback=?';
    // console.log(url);
}, function(error) {
    console.log("Something went wrong: ", error);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM