2017-05-04 39 views
-1

我正在使用MeteorJS 1.4.4.2 + ReactJS構建堵車應用程序。我使用的氣氛谷歌地圖包之前和流行的谷歌地圖npm包(istarkov和tomchentw)2,這些包工作正常,但它沒有我想要的,所以,而是我直接使用谷歌地圖API,以獲得它的完整功能。谷歌地圖API,未捕獲的錯誤,marker.setMap不是函數

我希望能夠在地圖上刪除選定的標記

學嘗試:

// Map functions in the same file as mapInit function 

let addMarker = (location)=> { 

    let marker = new google.maps.Marker({ 

     position: location, 
     map: map, 
     animation: google.maps.Animation.DROP, 
     draggable: true 

    }).addListener('click', (e)=> removeMarkerOnClick(e)); 

    markers.push(marker); 

}; 

let removeMarkerOnClick(marker)=> { 

    let lat = marker.latLng.lat(), 
     lng = marker.latLng.ln(), 
     position = {}; 

    markers = markers.reduce((new_markers, marker)=> { 

     position = marker.f.position; 

     position.lat() !== lat && position.lng() !== lng ? 

      new_markers.push(marker) : marker.setMap(null); 

     return new_markers; 

    }, markers); 

} 

window.initMap =() => { 

    //.... body hidden 

    map.addListener('click', (e)=> addMarker(e.latLng)); 

} 

錯誤從客戶端控制檯:

Uncaught TypeError: 
    MapFunctions.jsx:74 
    marker.setMap is not a function 
    at http://localhost:3000/app/app.js?hash=1f01aac45aac6af0dd009bc4623183b2511f62bf:419:20 
at Array.reduce (native) 
at removeMarkerOnClick (http://localhost:3000/app/app.js?hash=1f01aac45aac6af0dd009bc4623183b2511f62bf:413:23) 
at _.Ge.<anonymous> (http://localhost:3000/app/app.js?hash=1f01aac45aac6af0dd009bc4623183b2511f62bf:403:16) 
at Object._.z.trigger (https://maps.googleapis.com/maps/api/js?key=AIzaSyB1-fwP96l-_VAicaDGRNMmEU93TY4fcGs&libraries=places&callback=initMap:102:121) 
at XT.<anonymous> (https://maps.googleapis.com/maps-api-v3/api/js/28/14/marker.js:19:348) 
at _.pG._.z.trigger (https://maps.googleapis.com/maps/api/js?key=AIzaSyB1-fwP96l-_VAicaDGRNMmEU93TY4fcGs&libraries=places&callback=initMap:102:121) 
at _.pG.onclick (https://maps.googleapis.com/maps/api/js?key=AIzaSyB1-fwP96l-_VAicaDGRNMmEU93TY4fcGs&libraries=places&callback=initMap:40:81) 
at _.Bu._.z.trigger (https://maps.googleapis.com/maps/api/js?key=AIzaSyB1-fwP96l-_VAicaDGRNMmEU93TY4fcGs&libraries=places&callback=initMap:102:121) 
at _.Bu.<anonymous> (https://maps.googleapis.com/maps/api/js?key=AIzaSyB1-fwP96l-_VAicaDGRNMmEU93TY4fcGs&libraries=places&callback=initMap:40:81) 

解決方案的嘗試:

  1. 使用setOnMap(空)也無法正常工作,同樣的類型錯誤,setOnMap()不是一個函數

  2. 使用marker.setVisibility(假)也無法正常工作,同類型錯誤,......不是功能

我周圍的谷歌搜索一個半小時,也沒有找到任何東西,所以請大家幫忙,並非常感謝剛剛花費時間閱讀本。

回答

0

對不起,如果有人試圖找到解決這個問題的麻煩。 當我嘗試搜索Marker對象時,我嘗試使用其他標記示例,控制檯記錄標記對象以檢查差異,然後發現我的標記對象位於另一個頂級對象中,因此爲了訪問setMap函數,我必須去一個較低的水平。 解決方案:

let removeMarkerOnClick(marker)=> { 

    let lat = marker.latLng.lat(), 
     lng = marker.latLng.ln(), 
     position = {}; 

    markers = markers.reduce((new_markers, marker)=> { 

     position = marker.f.position; 

     position.lat() !== lat && position.lng() !== lng ? 

     new_markers.push(marker) : marker.f.setMap (null); // Change this line from marker.setMap(null) to marker.f.setMap (null) 

     return new_markers; 

    }, markers); 

} 
0

您在函數調用中傳遞事件。

}).addListener('click', (e)=> removeMarkerOnClick(e)); 

you should do: 
}).addListener('click', (e)=> removeMarkerOnClick(marker)); 
+0

哦,是的,對不起,我沒有看到來了,非常感謝你,我會改正,並儘快向您報告 – ShadowLegend

+0

你好兄弟,經過標記,這只是以獲得它的座標,但它仍然不能解決導致marker.setMap()不是函數的問題 – ShadowLegend

相關問題