javascript - Google: TypeError: google.maps.Geocoder is not a constructor - Stack Overflow

The following code throw exception as titled in firefox.<script type="textjavascript">

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745042646a4607903.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信