【问题标题】:google maps Cannot read property 'maps' of undefined谷歌地图无法读取未定义的属性“地图”
【发布时间】:2017-04-03 18:58:33
【问题描述】:

我尝试根据用户位置显示标记,但我不断收到此错误,因为我没有找到任何符合我的问题的解决方案, 我得到的错误是:未捕获的类型错误:无法读取未定义的属性“地图”及其指的是这个:在 Array.filter (native)。 谢谢:

  <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/

【问题讨论】:

  • 更正了您的小提琴 jsfiddle.net/vw5tee04 中的语法错误。您需要提供 google api 密钥才能使其工作

标签: javascript asp.net-mvc google-maps google-maps-api-3 geolocation


【解决方案1】:

您的 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

【讨论】:

  • 它确实有效,而且我的位置,但我不能在这里(和以前一样)数据:> var markers = @Html.Raw(Json.Encode(Model.UpcomingLectureGigs));我的 chrome 控制台错误是 Uncaught TypeError: markers.filter is not a function> var markersFiltered = markers.filter(function (markers, index, array, google)
  • 用你的整个代码测试这个,并从你的 Model.UpcomingLectureGigs 传递标记
  • 我不确定,但也只是我得到的代码(索引):206 Uncaught TypeError:markers.filter 不是函数(...)&我认为这就是为什么我不能使用我的“模型” .UpcomingLectureGigs”。
  • 我绝对无法在您的代码中调整我的模型,如果您能在这里帮助我,我将不胜感激。
  • 检查更新的小提琴。只有当您有动态“标记”具有标记。纬度,标记。经度属性时,您的过滤功能才会起作用。
猜你喜欢
  • 1970-01-01
  • 2017-11-09
  • 1970-01-01
  • 1970-01-01
  • 2021-11-03
  • 2018-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多