【问题标题】:Google maps v3 Error谷歌地图 v3 错误
【发布时间】:2016-07-29 05:59:13
【问题描述】:

我收到谷歌地图错误“谷歌未定义”

我正在使用带有 API 密钥的谷歌地图 V3 有人可以在这里指出我的问题吗?

我之前使用没有 API 密钥的谷歌地图,它工作正常。但在我添加 API 密钥后,它似乎不起作用。

<script async defer src="https://maps.googleapis.com/maps/api/js?key=MY-API-KEY&callback=initMap"></script>

<script>
    $(document).ready(function() {
      // initialize Google Maps
      initialize();
      // save marker to database
      $('#submitButton').click(function() {
        // we read the position of the marker and send it via AJAX
        var position = marker.getPosition();
        $.ajax({
          url: 'update_map.php',
          type: 'post',
          data: {
            lat: position.lat(),
            lng: position.lng(),
            id : <?php echo $id;?>
          },
          success: function(response) {
            // we print the INSERT query to #display
            $('#output').html(response);
          }
        });
      });

    });




    var map = null;
    var marker = null;

    // Google Maps
    function initialize() {
      var startDragPosition = null;
      var mapOptions = {
        zoom: 15,
        center: new google.maps.LatLng(<?php echo $lat;?>, <?php echo $long;?>),  // Over Belgium
        mapTypeId: google.maps.MapTypeId.TERRAIN
      };
      map = new google.maps.Map(document.getElementById('map-big'), mapOptions);
      // set the new marker
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(<?php echo $lat;?>, <?php echo $long;?>),
        map: map,
        draggable: true
      });

      var myGeocoder = new google.maps.Geocoder();

      // set a callback for the start and end of dragging
      google.maps.event.addListener(marker,'dragstart',function(event) {
        // we remember the position from which the marker started.  
        // If the marker is dropped in an other country, we will set the marker back to this position
        startDragPosition = marker.getPosition();
      });
      google.maps.event.addListener(marker,'dragend',function(event) {
        // now we have to see if the country is the right country.  
        myGeocoder.geocode({'latLng': marker.getPosition()}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK && results[1]) {
            var countryMarker = addresComponent('country', results[1], true);

          }
          else {
            // geocoder didn't find anything.  So let's presume the position is invalid
            marker.setPosition(startDragPosition);
          }
        });
      });
    }

    function addresComponent(type, geocodeResponse, shortName) {
      for(var i=0; i < geocodeResponse.address_components.length; i++) {
        for (var j=0; j < geocodeResponse.address_components[i].types.length; j++) {
          if (geocodeResponse.address_components[i].types[j] == type) {
            if (shortName) {
              return geocodeResponse.address_components[i].short_name;
            }
            else {
              return geocodeResponse.address_components[i].long_name;
            }
          }
        }
      }
      return '';
    }
</script?

【问题讨论】:

    标签: javascript google-maps google-maps-api-3


    【解决方案1】:

    您在脚本标记中调用callback=initMap,但您的javascript 代码中没有任何initMap 函数。相反,有一个initialize() 函数。 我想你应该改变两者之一

    【讨论】:

    • 你是救生员。谢谢你,先生。它就像一个魅力。
    猜你喜欢
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多