我正在尝试使用 Google Maps API 进行搜索。如果我在浏览器上打开这个 url(指定我的 API KEY),我会得到一个显示的结果。我正在使用 Phonegap & jQuery,并尝试在此函数中使用它:
$("#search").click(function() {
try {
$.mobile.showPageLoadingMsg();
navigator.geolocation.getCurrentPosition(function(position) {
var radius = $("#range").val() * 1000;
mapdata = new google.maps.LatLng( position.coords.latitude, position.coords.longitude );
var url = "https://maps.googleapis.com/maps/api/place/search/json?location=" +
position.coords.latitude + "," + position.coords.longitude + "&radius=" + radius +
"&name=" + $("#searchbox").val() + "&sensor=true&key=API_KEY";
$.getJSON(url,
function(data) {
cachedData = data;
$("#result-list").html("");
try {
$(data.results).each(function(index, entry) {
var htmlData = "<a href=\"#details\" id=\"" + entry.reference +"\"><img src\"" +
entry.icon + "\" class=\"ui-li-icon\"/img><h3> " + entry.name +
"</h3><p><strong> vicinity: " + entry.vicinity + "</strong></p></a>";
var liElem = $( document.createElement( 'li' ) );
$("#result-list").append(liElem.html( htmlData ));
$(liElem).bind( "tap",
function(event){
event.stopPropagation();
fetchDetails(entry);
return true;
});
});
$("#result-list").listview('refresh');
} catch(err) {
console.log( "Got error while putting search result on result page " + err );
}
$.mobile.changePage("list");
$.mobile.hidePageLoadingMsg();
}).error(function(xhr, textStatus, errorThrown) {
console.log("Got error while fetching search result : xhr.status=" + xhr.status);
}).complete(function(error) {
$.mobile.hidePageLoadingMsg();
});
},
function(error) {
console.log("Got Error fetching geolocation " + error);
});
} catch(err){
console.log("Got error on clicking search button "+ err);
}
});
在测试时,我在我的萤火虫控制台上得到了这个响应:
GET https://maps.googleapis.com/maps/api/place/search/json?location=-26.1443912,28.0261926&radius=5000&name=Pizza&sensor=false&key=API_KEY 200 OK (colored red with an error icon next to it)<br/>
获取搜索结果时出错:xhr.status = 0