2011-12-17 80 views
2

我正在使用html5和java腳本創建一個簡單的應用程序。 我想顯示任何一個城市最近的地方 所以我已經設置了一個國家的長期和緯度。如何獲得放置在Google地圖上的JSON結果?

如果用戶點擊學校,我想顯示所有學校名稱與半徑最接近的長和長。
如果用戶點擊教堂,我想顯示所有教會名稱與半徑最接近的長和長。
如果用戶點擊醫院,我想顯示所有醫院名稱最接近半徑爲long和lat的醫院名稱。等

我只有這個鏈接和示例代碼有關:

https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AIzaSyC1BIAzM34uk6SLY40s-nmXMivPJDfWgTc 


$.getJSON("https://maps.googleapis.com/maps/api/place/search/json?",{ 
     location:"-33.8670522,151.1957362", 
     radius: 500, 
     types:'food', 
     name: 'harbour', 
     sensor: false, 
     key: 'AIzaSyC1BIAzM34uk6SLY40s-nmXMivPJDfWgTc' 
     format: "json" 
    }, 
    function(data) { 
     $.each(data.results, function(i,item){ 
     $("<img/>").attr("src", results.name).appendTo("#images"); 
     if (i == 3) return false; 
    }); 
    }); 

所以在這裏我要改變得到地名。 最初我該怎麼辦?

+0

請參閱更新的答案。有錯別字 – mplungjan 2011-12-22 09:37:45

回答

1

地圖V3不支持回撥/ JSONP從一個jQuery獲得/的getJSON此時

http://www.quora.com/Why-doesnt-the-Google-Maps-API-support-JSONP

那說 - 如果你有耐心嘗試尋找

http://code.google.com/intl/no-NO/apis/maps/documentation/javascript/services.html#Geocoding

加載異步,你需要做這樣的事情:

function loadScript() { 
    var script = document.createElement("script"); 
    script.type = "text/javascript"; 
    script.src = "http://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize"; 
    document.body.appendChild(script); 
} 

http://code.google.com/apis/maps/documentation/javascript/basics.html#Async


如果不是,這裏是我的建議。

使用代理:

除非有人能找到新的資源,顯示瞭如何用JSONP做到這一點(我只能找到資源告訴我,這是不支持)的方式就是寫一個代理。

此作品來自同一服務器

DEMO

HTML:

<html> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
<script> 
$(document).ready(function() { 
    $.getJSON("googleapijson.php?type=food", 
    function(data, textStatus){ 
     $.each(data.results,function(i, item) {; 
     $("#placenames").append(i+':'+item.vicinity+'<br/>'); 
    }); 
    }); 
}); 
</script> 
</head> 
<body> 
<div id="placenames"></div> 
</body> 
</html> 

PHP:

<?PHP 

header("Content-type: application/json"); 

/** note you need to grab and clean the parameters from the 
* get/post request and build your querystring: 
*/ 

$type = $_GET["type"]; // this needs to be cleaned 

$URL="https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=".$type."&name=harbour&sensor=false&key=AIzaSyC1BIAzM34uk6SLY40s-nmXMivPJDfWgTc" 

$json = file_get_contents($URL); 

echo $json; 
?> 
+0

非常感謝。 – SANR1103219 2012-01-03 07:18:38

相關問題