2017-10-10 32 views
-1

我創建了谷歌地圖使用JSON和使用的數據庫PostgreSQL的顯示標記谷歌地圖API無法使用JSON(V3)

這是我的代碼

<script type="text/javascript"> 
    var map; 

    var national = [{"lng":"-6.173319","city":"JAKARTA","lat":"106.818672"}]; 

    function initialize() { 
    var mapOptions = { 
     center: new google.maps.LatLng(-0.789275,113.921327), 
     zoom: 5, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var infoWindow = new google.maps.InfoWindow(); 
    var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); 

    $.getJSON('json_city.json', function(data) { 
    $.each(data.national, function(index, item) { 
     var myLatlng = new google.maps.LatLng(item.lat, item.lng); 
     alert(myLatlng); 
     var marker = new google.maps.Marker({ 
     position: myLatlng, 
     map: map, 
     title: "text "+item.city 
     }); 
    }); 
    }); 

    // Initialize the map 
    } 
    google.maps.event.addDomListener(window, 'load', initialize); 

</script> 

緯度和長期使用的字符類型(文本) 。我的地圖可以顯示,但我的標記不能顯示。

+1

你在你的'國家'變量中有一個錯字; 「lat」和「lng」向後(應該是「lat」:「-6.173319」,「lng」:「106.818672」)。 ([小提琴](http://jsfiddle.net/geocodezip/f3rwcdyd/)) – geocodezip

+0

感謝您的更正@geocodezip –

回答

0

Google Maps需要LatLng構造函數的座標是Numbers而不是字符串。嘗試:

var myLatlng = new google.maps.LatLng(parseFloat(item.lat), parseFloat(item.lng)); 
+0

我試過了,但沒有奏效。 我已經check in console.log(「item.lat」);和console.log(「item.lng」);而我的谷歌瀏覽器無法顯示日誌。 @duncan –

+0

'data.national'是什麼樣的?你已經在你的JS的頂部硬編碼了'var national',但它沒有被使用。 – duncan