admin管理员组

文章数量:1410705

The following code throw exception as titled in firefox.

<script type="text/javascript">
$(function () {
initialize();
});
var geocoder;
function initialize() {
geocoder = new google.maps.Geocoder();
}

The following code throw exception as titled in firefox.

<script type="text/javascript">
$(function () {
initialize();
});
var geocoder;
function initialize() {
geocoder = new google.maps.Geocoder();
}

Share Improve this question edited Feb 27, 2013 at 9:40 Thomas 2,7775 gold badges32 silver badges52 bronze badges asked Feb 27, 2013 at 9:21 user2114637user2114637 211 gold badge1 silver badge2 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 1

My guess is that you didn't include Google Map's header, because your code seems correct.

Try following the steps here.

With the newest way to import a library, the geocoder() method may not be in the maps anymore.

Let say you have the following

  const promises = [
      mapLoader.importLibrary('maps'),
      mapLoader.importLibrary('geocoding'),
    ]

    // Use Promise.all() to send the promises in parallel
    return Promise.all(promises)
      .then(([maps, geocoding, places, core]) => {
        // Return the results as a single object named 'google'
        return {
          maps,
          geocoding,
        }
      })
      .catch((error) => {})

Then it will be geocoder = new google.geocoding.Geocoder();

You should call the constructor after the body has been loaded. document.body.addEventListener('load', initialize)

add this before calling functions that consists of geocoder to ensure the specific constructor is intialized.

google.maps.importLibrary("geocoding").then(() => { 
    //Call your function here 
}

If google map script is not loeaded, and you call map related function then you would get this kind of errors.

本文标签: javascriptGoogle TypeError googlemapsGeocoder is not a constructorStack Overflow