Google Map Api Uncaught TypeError: Cannot read property '0' of null

妖精的绣舞 提交于 2019-12-20 07:47:51

问题


I am face the problem Uncaught TypeError: Cannot read property '0' of null on converting the address to latitude and longitude this the my code

The code is

function convertAddress( address, callback, item ) {
    geocoder.geocode( { 
        'address' : address 
    }, function(results, status) {
        if ( status == google.maps.GeocoderStatus.OK ) {
            callback( results[0].geometry.location );
        }

        cords = [ results[0].geometry.location.pb, results[0].geometry.location.qb ];

        $.post( jobifySettings.ajaxurl, { action : 'jobify_cache_cords', cords : cords, job : item.job } );
    });
}

回答1:


  1. don't use undocumented properties (location.pb, location.qb)
  2. don't put code that depends on a successful result outside of the check for success

    function convertAddress( address, callback, item ) {
      geocoder.geocode( { 
        'address' : address 
      }, function(results, status) {
        if ( status == google.maps.GeocoderStatus.OK ) {
          callback( results[0].geometry.location );
          cords = [ results[0].geometry.location.lat(), results[0].geometry.location.lng() ];
          $.post( jobifySettings.ajaxurl, { action : 'jobify_cache_cords', cords : cords, job : item.job } );
        } else alert("Geocode failed, status: "+status);
      });
    }
    



回答2:


if you do not have a business account, it could also give you this error which comes from this response of the server:

{
  "error_message" : "You have exceeded your rate-limit for this API.",
  "results" : [],
  "status" : "OVER_QUERY_LIMIT"
}

Which means that either you load the api too many times a day/too concentrated or that you try to load many addresses.

This particular response will also cause this error.

 Uncaught TypeError: Cannot read property 'geometry' of undefined

Here you can find some tips that might help. https://developers.google.com/maps/documentation/business/articles/usage_limits



来源:https://stackoverflow.com/questions/22442876/google-map-api-uncaught-typeerror-cannot-read-property-0-of-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!