3

我試圖根據用戶位置顯示標記,我一直得到這個錯誤,因爲它是我沒有找到任何解決方案,匹配我的問題, 我錯誤的getiing是:Uncaught TypeError:無法讀取屬性'maps'的未定義&其引用此:at Array.filter(native)。 感謝:谷歌地圖無法讀取屬性的'地圖'的undefined

<script src="https://maps.googleapis.com/maps/api/js?key="My_Api_Key"&libraries=geometry"></script> 
<script> 
    "use strict"; 
    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(
      function(position) { 

       var markers = @Html.Raw(Json.Encode(Model.UpcomingLectureGigs)); 
       var currentLatitude = position.coords.latitude; 
       var currentLongitude = position.coords.longitude; 
       var userLatLng = currentLatitude + currentLongitude; 
       var markersFiltered = markers.filter(function(markers, index, array, google) { 
       var myLatlng = new window.google.maps.LatLng(markers.Latitude, markers.Longitude); 

        return google.maps.geometry.spherical.computeDistanceBetween(userLatLng, myLatlng) < 10000; 
       }); 
       window.onload = function(a) { 
        var mapOptions = { 
         center: new window.google.maps.LatLng(markers[0].Latitude, markers[0].Longitude), 

         zoom: 8, 
         mapTypeId: window.google.maps.MapTypeId.ROADMAP 
        }; 

        var infoWindow = new window.google.maps.InfoWindow(); 
        var map = new window.google.maps.Map(document.getElementById("dvMap"), mapOptions); 

        for (var i = 0; i < markersFiltered.length; i++) { 
         var data = markers[i]; 
         var myLatlng = new window.google.maps.LatLng(data.Latitude, data.Longitude); 
         var marker = new window.google.maps.Marker({ 
          position: myLatlng, 
          draggable: true, 
          animation: google.maps.Animation.DROP, 
          get map() { return map; } 
         }); 
         (function(marker, data) { 
          window.google.maps.event.addListener(marker, 
           "click", 
           function(e) { 
            infoWindow.setContent(data.Venue + " " + data.Genre.Name); 
            infoWindow.open(map, marker); 
           }); 
         })(marker, data); 
        } 

       }; 
      }); 
    }; 
</script> 

https://jsfiddle.net/1gm0u4wd/9/

+0

糾正你的提琴https://jsfiddle.net/vw5tee04/語法錯誤。你需要提供谷歌的API密鑰,以使其工作 – Deep

回答

4

你的HTML將如下所示。您的地圖api javascript將鏈接在html部分而不是javascript部分。

<div id="map" style="width: 500px; height: 500px"/> 

<script src="https://maps.googleapis.com/maps/api/js?key=yourMapsAPIKey&libraries=geometry"></script> 

注意:「yourMapsAPIKey」應該是您的API密鑰。 您可以從here獲取您的地圖api密鑰。

更新腳本如下

"use strict"; 
if (navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(
    function(position) { 
     var currentLatitude = position.coords.latitude; 
     var currentLongitude = position.coords.longitude; 
     var map = new google.maps.Map(document.getElementById('map'), { 
     zoom: 4, 
     center: new google.maps.LatLng(currentLatitude, currentLongitude), 
     }); 

     //your dynamic markers 
     var markers = @Html.Raw(Json.Encode(Model.UpcomingLectureGigs)); 

     //default marker at current location 
     //var markers = new google.maps.Marker({ 
     //position: new google.maps.LatLng(currentLatitude, currentLongitude), 
     //map: map, 
     //title: 'Hello World!' 
     //}); 

     var userLatLng = new window.google.maps.LatLng(currentLatitude, currentLongitude); 

     //your filter function 
     var markersFiltered = markers.filter(function(markers, index, array, google) { 
     var myLatlng = new window.google.maps.LatLng(markers.Latitude, markers.Longitude); 
     return google.maps.geometry.spherical.computeDistanceBetween(userLatLng, myLatlng) < 10000; 
     }); 

     window.onload = function(a) { 
     var mapOptions = { 
      center: new window.google.maps.LatLng(markers[0].Latitude, markers[0].Longitude), 
      zoom: 8, 
      mapTypeId: window.google.maps.MapTypeId.ROADMAP 
     }; 

     var infoWindow = new window.google.maps.InfoWindow(); 
     var map = new window.google.maps.Map(document.getElementById("dvMap"), mapOptions); 

     for (var i = 0; i < markersFiltered.length; i++) { 
      var data = markers[i]; 
      var myLatlng = new window.google.maps.LatLng(data.Latitude, data.Longitude); 
      var marker = new window.google.maps.Marker({ 
      position: myLatlng, 
      draggable: true, 
      animation: google.maps.Animation.DROP, 
      get map() { 
       return map; 
      } 
      }); 
      (function(marker, data) { 
      window.google.maps.event.addListener(marker, 
       "click", 
       function(e) { 
       infoWindow.setContent(data.Venue + " " + data.Genre.Name); 
       infoWindow.open(map, marker); 
       }); 
      })(marker, data); 
     } 

     }; 
    }); 
}; 

Fiddle here

+0

它的工作和ic的位置,但我在這裏(我的數據)我的數據(如befor):var tags = @ Html.Raw(Json.Encode(Model.UpcomingLectureGigs) );我的Chrome控制檯錯誤是未捕獲TypeError:markers.filter不是一個函數> var markersFiltered = markers.filter(函數(markers,index,array,google) –

+0

用你的整個代碼測試這個,並從你的Model.UpcomingLectureGigs – nitin

+0

林不知道,但也只是你的代碼我得到(索引):206未捕獲TypeError:markers.filter是不是一個函數(...)我認爲這就是爲什麼我不能使用我的「Model.UpcomingLectureGigs」 –

相關問題