2016-03-19 64 views
0

我已經創建了一個應用程序,允許用戶在點擊事件後在谷歌地圖上繪製一個標誌,但是一旦刷新頁面,所有的標誌都會丟失。我希望能夠保持用戶使用本地存儲輸入的數據,任何人都可以指向我的方向或向我展示如何處理這個問題的示例代碼?謝謝。

基本谷歌地圖沒有本地存儲代碼繪製谷歌地圖與本地存儲

var map; 
var myCenter=new google.maps.LatLng(54.906435, -1.383944); 

function initialize() 
{ 
var mapProp = { 
    center:myCenter, 
    zoom:13, 
    mapTypeId:google.maps.MapTypeId.ROADMAP 
    }; 

    map = new google.maps.Map(document.getElementById("googleMap"),mapProp); 

     //creating the event for placing the marker 
    google.maps.event.addListener(map, 'click', function(event) { 
    placeMarker(event.latLng); 
    }); 

} 
//Funcion to place the marker on the map (flag) 
function placeMarker(location) { 

    var marker = new google.maps.Marker({ 
    position: location, 
    icon:'flag.png', 
    map: map, 
    }); 
    //open information window once marker is placed 
    var infowindow = new google.maps.InfoWindow({ 
    content: 'User has placed warning' 
    }); 
    infowindow.open(map,marker); 

    //zoom into the marker 
    google.maps.event.addListener(marker,'click',function() { 
    map.setZoom(17); 
    map.setCenter(marker.getPosition()); 
    }); 

} 


google.maps.event.addDomListener(window, 'load', initialize); 

回答

0

這應該做的伎倆;)

function initialize() 
 
{ 
 
var mapProp = { 
 
    center:myCenter, 
 
    zoom:13, 
 
    mapTypeId:google.maps.MapTypeId.ROADMAP 
 
    }; 
 

 
    map = new google.maps.Map(document.getElementById("googleMap"),mapProp); 
 

 
     //creating the event for placing the marker 
 
    google.maps.event.addListener(map, 'click', function(event) { 
 
    writeToStorage(event.latLng); 
 
    placeMarker(event.latLng); 
 
    }); 
 
    
 
    setupStorage(); 
 
    readFromStorage(); 
 
} 
 

 
function setupStorage() { 
 
    if(typeof(localStorage) === "undefined") { 
 
    // If localStorage isn't supported, fake it. 
 
    // Won't persist between sessions. 
 
    localStorage = {}; 
 
    } 
 
    
 
    localStorage.locations = localStorage.locations || []; 
 
} 
 

 
function writeToStorage(location) { 
 
    localStorage.locations.push(location); 
 
} 
 

 
function readFromStorage() { 
 
    localStorage.locations.forEach(function(location) { 
 
    placeMarker(location); 
 
    }); 
 
} 
 

 
//Funcion to place the marker on the map (flag) 
 
function placeMarker(location) { 
 
    var marker = new google.maps.Marker({ 
 
    position: location, 
 
    icon:'flag.png', 
 
    map: map, 
 
    }); 
 
    //open information window once marker is placed 
 
    var infowindow = new google.maps.InfoWindow({ 
 
    content: 'User has placed warning' 
 
    }); 
 
    infowindow.open(map,marker); 
 

 
    //zoom into the marker 
 
    google.maps.event.addListener(marker,'click',function() { 
 
    map.setZoom(17); 
 
    map.setCenter(marker.getPosition()); 
 
    }); 
 

 
} 
 

 

 
google.maps.event.addDomListener(window, 'load', initialize);

讓我知道,如果您有任何理解的任何麻煩它。

+0

感謝您的回覆,當我使用該代碼地圖顯示正確的縮放,但我不能繪製任何東西? –

+0

如果可以,請設置jsfiddle。同時檢查你的控制檯日誌,我有一種感覺,可能在初始化之前繪製點。 –

+0

我已更新我的代碼,看看如何去... –