2013-12-20 67 views
1

你好我想在用戶輸入城市地圖時獲取地震信息。現在我通過硬編碼(geonames earthquake api要求)來製作包圍盒。我想使邊界框與輸入的城市相同。我看到了地理編碼API V3做到這一點:如何在geonames和geocoder api中動態生成邊界框

var geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 
     map.fitBounds(results[0].geometry.viewport); 
     } 
    }); 

problem-這不會讓我得到東北西南它GEONAMES需求。有誰知道如何?

我當前的代碼

var address = document.getElementById("address").value; 
      geocoder.geocode({ 'address': address}, function(results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 
        // Calculate bounding box based on the given address 
        var north = results[0].geometry.location.lat() + .5; 
        var south = results[0].geometry.location.lat() - .5; 
        var east = results[0].geometry.location.lng() + .5; 
        var west = results[0].geometry.location.lng() - .5; 

        var earthquake = 'http://api.geonames.org/earthquakesJSON?north=' + north + '&south=' + south + '&east=' + east + '&west=' + west + '&username=*****'; 

+5 -5是在什麼樣的座標擴展了數字到南向北創建....等

回答

1

您要使用的結果[0 ] .geometry.viewport其爲google.maps.LatLngBounds object

var north = results[0].geometry.viewport.getNorthEast().lat(); 
var south = results[0].geometry.viewport.getSouthWest().lat(); 
var east = results[0].geometry.viewport.getNorthEast().lng(); 
var west = results[0].geometry.viewport.getSouthWest().lng(); 

fixed fiddle

fiddle with return message from geonames

fiddle with hard-coded data that shows markers

+0

感謝你爲這個我一直在尋找在文檔很長一段時間笑 –

+0

我有一個問題。我的舊代碼現在使用latlngbounds對象的每個地震的標記,他們不久有任何原因,這可能是什麼? –

+0

不知道。什麼是您發送的實際URL?你得到任何JavaScript錯誤?顯示地震的代碼是什麼樣的? – geocodezip