调用地理名称查找附近的街道

时间:2015-11-01 18:30:16

标签: javascript

我正在尝试使用geonames Web服务获取LatLng值的附近街道列表。我可以获得附近的wikopedia文章,但无法使用findNearbyStreetsOSM方法获取街道名称列表。这就是我所拥有的: 我是盲人并使用屏幕阅读器。希望代码缩进正确。

//geonames API function 
//feed in the geonames method i.e. findNearbyWikipedia, findNearbyStreetsOSM + the long and lat values + the name of the div you want the results to be displayed in 
function FetchDataFromGeoNames(geonamesMethod, latitude, longitude, divToOutputResultsTo) {
  var geonamesAPIKey = "my_API_key);
  var apiUrl = "http://api.geonames.org/";
  var radius = 1;

  var request = apiUrl + geonamesMethod + "JSON?lat=" + latitude + "&lng=" + longitude + "&username=" + geonamesAPIKey;
  if (geonamesMethod == 'findNearbyWikipedia') {
    request += "&radius=" + radius + "&maxRows=5&country=UK";
  } 
  request += '&callback=?';
  //alert(request);

  //pass the request onto geonames API
  $.getJSON(request, {}, function(res) {
    if (res.hasOwnProperty("status")) {
      $("#divToOutputResultsTo").html("Sorry, I failed to work because: " + res.status.message);
      return;
    }

    var s = "";
    //loop through the results 
    for (var i = 0; i < res.geonames.length; i++) {
    //alert(JSON.stringify(res));

    //if find wikopedia request then 
    if (geonamesMethod == 'findNearbyWikipedia') {
      s += "<p><h2>" + res.geonames[i].title;
    } else if (geonamesMethod == 'findNearbyStreetsOSM') {
      s += "<p><h2>" + res.geonames[i].name;
    }

    if (geonamesMethod == 'findNearbyWikipedia') {
      if(res.geonames[i].hasOwnProperty("thumbnailImg")) s += "<img src='"+res.geonames[i].thumbnailImg+"' align='left'>";
      if (!!res.geonames[i].feature && res.geonames[i].feature != "undefined") s += '<br />Feature: ' + res.geonames[i].feature;
      s += '<br />' + res.geonames[i].summary;
      s += "<br clear='left'><a href='http://"+res.geonames[i].wikipediaUrl+"'>[Read More]</a></p>";
    } else if (geonamesMethod == 'findNearbyStreetsOSM') {
      s += '<br />Highway: ' + res.geonames[i].highway;
    }
  }


  //concatonate the results
  if (s != "") {
    if (geonamesMethod == 'findNearbyWikipedia') {
      s = "<h2>Nearby Wikopedia</h2>" + s;
    } else if (geonamesMethod == 'findNearbyStreetsOSM') {
      s = "<h2>Nearby streets (OSM)</h2>" + s;
    }

    //display the results on screen 
    $(divToOutputResultsTo).html(s);
  }

  });
}

谢谢,

0 个答案:

没有答案