2015-09-07 51 views
1

我正在開發一個jQuery移動地圖應用,我有map.html,map.js和map.json文件下面如何從JSON文件加載多個標記?

function initialize() { 
 
     var latitude = 57.95, 
 
      longitude = 14.65, 
 
      radius = 8000, //how is this set up 
 
      center = new google.maps.LatLng(latitude,longitude), 
 
      bounds = new google.maps.Circle({center: center, radius: radius}).getBounds(), 
 
      mapOptions = { 
 
       center: center, 
 
       zoom: 9, 
 
       mapTypeId: google.maps.MapTypeId.ROADMAP, 
 
       scrollwheel: false 
 
      }; 
 

 
     var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); 
 

 
     setMarkers(center, radius, map); 
 
    } 
 

 
    function setMarkers(center, radius, map) { 
 
     var json = (function() { 
 
      var json = null; 
 
      $.ajax({ 
 
       'async': false, 
 
       'global': false, 
 
       'url': "./map.json", 
 
       'dataType': "json", 
 
       'success': function (data) { 
 
        json = data; 
 
       } 
 
      }); 
 
      return json; 
 
     })(); 
 

 
     var circle = new google.maps.Circle({ 
 
       strokeColor: '#000000', 
 
       strokeOpacity: 0.25, 
 
       strokeWeight: 1.0, 
 
       fillColor: '#ffffff', 
 
       fillOpacity: 0.1, 
 
       clickable: false, 
 
       map: map, 
 
       center: center, 
 
       radius: radius 
 
      }); 
 
     var bounds = circle.getBounds(); 
 

 
     //loop between each of the json elements 
 
     for (var i = 0, length = json.length; i < length; i++) { 
 
      var data = json[i], 
 
      latLng = new google.maps.LatLng(data.lat, data.lng); 
 

 

 

 
      if(bounds.contains(latLng)) { 
 
       // Creating a marker and putting it on the map 
 
       var marker = new google.maps.Marker({ 
 
        position: latLng, 
 
        map: map, 
 
        title: data.content 
 
       }); 
 
       infoBox(map, marker, data); 
 
      } 
 
     } 
 

 
     circle.bindTo('center', marker, 'position'); 
 
    } 
 

 
    function infoBox(map, marker, data) { 
 
     var infoWindow = new google.maps.InfoWindow(); 
 
     // Attaching a click event to the current marker 
 
     google.maps.event.addListener(marker, "click", function(e) { 
 
      infoWindow.setContent(data.content); 
 
      infoWindow.open(map, marker); 
 
     }); 
 

 
     // Creating a closure to retain the correct data 
 
     // Note how I pass the current data in the loop into the closure (marker, data) 
 
     (function(marker, data) { 
 
      // Attaching a click event to the current marker 
 
      google.maps.event.addListener(marker, "click", function(e) { 
 
      infoWindow.setContent(data.content); 
 
      infoWindow.open(map, marker); 
 
      }); 
 
     })(marker, data); 
 
    } 
 

 
    google.maps.event.addDomListener(window, 'load', initialize);
[{ 
 
    "lat": 57.95, 
 
    "lng": 14.65, 
 
    "content":"test content1" 
 
}, 
 
{ 
 
    "lat": 57.9, 
 
    "lng": 14.6, 
 
    "content":"test content2" 
 
}, 
 
{ 
 
    "lat": 57.85, 
 
    "lng": 14.55, 
 
    "content":"test content3" 
 
}]
<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
 
    <style type="text/css"> 
 
     html { height: 100% } 
 
     body { height: 100%; margin: 0; padding: 0 } 
 
     #map-canvas { height: 100% } 
 
    </style> 
 
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
 
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 
 
    <script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script> 
 
    </script> 
 
    <script src="./map.js"></script> 
 
    </head> 
 
    <body> 
 
    <div id="map-canvas"/> 
 
    </body> 
 
</html>

給出,但在我的代碼標記都不見....當我試圖運行一個單一的HTML文件沒有任何外部.js和.json文件它的工作,但與此代碼,我無法運行..

我需要知道哪裏會出錯,也neeed help: 1.在地圖上顯示多個標記 2.點擊它應該顯示在內容標籤

等待救援消息的標誌...在此先感謝...

+0

您的瀏覽器控制檯是否顯示任何錯誤? – Mayhem

+0

這是我得到的錯誤 – Rachna

+1

XMLHttpRequest無法加載file:/// C:/Users/Desktop/map/map.json。協議方案只支持交叉原點請求:http,data,chrome,chrome-extension,https,chrome-extension-resource.b.ajaxTransport.send @ jquery-1.9.1.min.js:5b.extend.ajax @ jquery-1.9.1.min.js:5(匿名函數)@ map.js:22setMarkers @ map.js:32initialize @ map.js:16 map.js:48 Uncaught TypeError:無法讀取nullsetMarkers的屬性'length' @ map.js:48initialize @map.js:16 – Rachna

回答

1

沒有什麼錯的例子中,最有可能的,爲什麼你的理由由於您試圖直接從瀏覽器打開html頁面(因此要通過文件協議加載map.json),因此出現此錯誤。

基本上有兩種方法可供選擇:

選項1

您可以安裝一個Web服務器和localhost訪問您的網頁或上傳map.json某處在網絡上和URL更改爲http://example.com/map.json

選項2

Al低的瀏覽器來訪問本地文件,例如在Chrome中,你可以指定allow-file-access-from-files標誌,例如:

> .\chrome.exe --allow-file-access-from-files 
0

請查看兩個解決這個代碼,

jQuery(function($) { 
// Asynchronously Load the map API 
var script = document.createElement('script'); 
script.src = "http://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize"; 
document.body.appendChild(script);}); 

function initialize() { 
    var map; 
    var bounds = new google.maps.LatLngBounds(); 
    var mapOptions = { 
     mapTypeId: 'roadmap' 
    }; 



// Display a map on the page 
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); 
    map.setTilt(45); 

// Multiple Markers 
var markers = [ 
    ['London Eye, London', 51.503454,-0.119562], 
    ['Palace of Westminster, London', 51.499633,-0.124755] 
]; 

// Info Window Content 
var infoWindowContent = [ 
    ['<div class="info_content">' + 
    '<h3>London Eye</h3>' + 
    '<p>The London Eye is a giant Ferris wheel situated on the banks of the River Thames. The entire structure is 135 metres (443 ft) tall and the wheel has a diameter of 120 metres (394 ft).</p>' +  '</div>'], 
    ['<div class="info_content">' + 
    '<h3>Palace of Westminster</h3>' + 
    '<p>The Palace of Westminster is the meeting place of the House of Commons and the House of Lords, the two houses of the Parliament of the United Kingdom. Commonly known as the Houses of Parliament after its tenants.</p>' + 
    '</div>'] 
]; 

// Display multiple markers on a map 
var infoWindow = new google.maps.InfoWindow(), marker, i; 

// Loop through our array of markers & place each one on the map 
for(i = 0; i < markers.length; i++) { 
    var position = new google.maps.LatLng(markers[i][1], markers[i][2]); 
    bounds.extend(position); 
    marker = new google.maps.Marker({ 
     position: position, 
     map: map, 
     title: markers[i][0] 
    }); 

    // Allow each marker to have an info window  
    google.maps.event.addListener(marker, 'click', (function(marker, i) { 
     return function() { 
      infoWindow.setContent(infoWindowContent[i][0]); 
      infoWindow.open(map, marker); 
     } 
    })(marker, i)); 

    // Automatically center the map fitting all markers on the screen 
    map.fitBounds(bounds); 
} 

// Override our map zoom level once our fitBounds function runs (Make sure it only runs once) 
var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) { 
    this.setZoom(14); 
    google.maps.event.removeListener(boundsListener); 
}); 

} 

參考鏈接:http://wrightshq.com/playground/placing-multiple-markers-on-a-google-map-using-api-3/

0

我發現了DataLayerGeoJSON功能塊是一種在地圖上繪製靜態點或區域的更簡單的方法。我建議使用map.data.loadGeoJson(url)調用替換您的數據文件與GeoJSON &。

DataLayer鏈接還介紹瞭如何加載URL。它談論CORS和你的問題。

0

已回答此問題, 在cmd中運行此命令,通過讓它訪問本地文件來啓動chrome。

「C:\ Program Files文件(x86)的\谷歌\鍍鉻\應用\的chrome.exe」 --allow-文件訪問從檔案

,其餘全部是正確的,我可以顯示多種標記和onclick一個信息窗口將打開..

爲了更好的可讀性,您可以更改latlang初始化和半徑。在上面的代碼片段中,只有2個標記可見,儘管json文件中有3個標記。增加半徑以查看第三個標記 感謝所有人的幫助

相關問題