简体   繁体   English

Google Maps v3错误

[英]Google maps v3 Error

I'm getting a Google maps error "google is not defined" 我收到Google地图错误“未定义Google”

I'm using google maps V3 with API key Can someone point me out the issue here. 我正在使用带有API密钥的Google Maps V3。有人可以在这里指出我的问题。

I was using google maps without API key before and it was working fine. 我之前使用的是不带API密钥的Google地图,并且运行良好。 But after i add the API key it doesn't seems to be working. 但是在我添加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?

You call callback=initMap in the script tag but there isn't any initMap function in your javascript code. 您在script标记中调用callback=initMap ,但是您的javascript代码中没有任何initMap函数。 Instead there is a initialize() function. 而是有一个initialize()函数。 I suppose you should change one of the two 我想你应该改变两者之一

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM