简体   繁体   中英

Unable to load google map while using jquery

Map loads successfully if I do not include jquery.min.js in the head tag. If I include it gives me initMap : no such method error. If I reverse the order of inclusion: first maps then jquery, the initMap method is never called back and map is not loaded. If I call initMap in document.ready, it gives me ReferenceError: google is not defined.

How do I work a way to use both Maps and jquery in same page.

<!DOCTYPE html>
<html>
<head>
<title>maps test</title>
<style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
}
</style>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"/>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBEn8E0rL_XdVl2WDLAwgjpM62AM2QoRXI&v=3.exp&sensor=false&libraries=places&callback=initMap"
    async defer></script>       

</head>

<body>${message}<br>
    <input id="location" type="text" value="Enter your location"/><br>  
     <select id="activity" value=" activity">
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="mercedes">Mercedes</option>
      <option value="audi">Audi</option>
    </select> 
    <div id="map"></div>    
    <script>
        var map;
    var autocomplete;
    var globalData;
    $(document).ready(function(){
            initMap();
    });
        function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: -34.397, lng: 150.644},
          zoom: 8
        });
        initialize();
    }
    function initialize() {
              autocomplete = new google.maps.places.Autocomplete(
                  /** @type {HTMLInputElement} */(document.getElementById('location')),
                  { types: ['geocode'] });
              google.maps.event.addListener(autocomplete, 'place_changed', loadMarkers);
        }
    function loadMarkers(){
        var place = autocomplete.getPlace();
        var loc = place.geometry.location;
        var address = place.address_components;
        $.ajax({
          type: "POST",
          dataType: "json",
          data: {loc : loc, address : address},
          url: '/getmarkers.do',
          success: function(data) {
            globalData = data;
                // get array of latlng
            // create marker

          }
        });
    }
    </script>

</body>
</html>

The script tag you are using to include JQuery is incorrect:

This:

 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"/>
 <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBEn8E0rL_XdVl2WDLAwgjpM62AM2QoRXI&v=3.exp&sensor=false&libraries=places&callback=initMap"
async defer></script> 

Should be (notice the /> that changed to ></script> :

 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
 <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBEn8E0rL_XdVl2WDLAwgjpM62AM2QoRXI&v=3.exp&sensor=false&libraries=places&callback=initMap"
async defer></script> 

Script tags must be <script></script> .

Try load google map api after jQuery is ready.

Example here .

Everything Works Fine You Just missed the Closing tag of jquery script ie </script> . Close the script and it works fine.

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"/></script>

Add this to your Code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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