最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Maps API v3 - google.maps.geometry.poly.containsLocation not working the first time - Stack Overflow

programmeradmin0浏览0评论

I am having the problem that google.maps.geometry.poly.containsLocation(latLng,polygon) does not work the first time when the page load, but it works later after I start to fire events.

Any idea of why this may be happening?

Thanks !

I am having the problem that google.maps.geometry.poly.containsLocation(latLng,polygon) does not work the first time when the page load, but it works later after I start to fire events.

Any idea of why this may be happening?

Thanks !

Share Improve this question asked Sep 12, 2013 at 15:45 Marcelo LagoMarcelo Lago 411 silver badge2 bronze badges 5
  • What error do you get in the JavaScript console? – Michael Geary Commented Sep 12, 2013 at 15:55
  • What does your code look like? – duncan Commented Sep 12, 2013 at 16:18
  • JavaScript console says: Uncaught TypeError: Cannot read property 'poly' of undefined – Marcelo Lago Commented Sep 12, 2013 at 16:49
  • Sounds like the first time you call it the API hasn't loaded yet. But as you haven't provided any code, that is just a guess. – geocodezip Commented Sep 12, 2013 at 17:01
  • 2 I found the source of the error ... I needed to specify "libraries=geometry" in the initial Google API call. See similar question at (stackoverflow./questions/10765667/…). Thanks for the tips ! – Marcelo Lago Commented Sep 12, 2013 at 17:06
Add a ment  | 

1 Answer 1

Reset to default 4

Main reason is at first time google api is not loaded properly and you are trying to use

var rsul = google.maps.geometry.poly.containsLocation(new google.maps.LatLng(-12.043333, -77.028333), bermudaTriangle);

so it is showing 'Cannot read property 'poly' of undefined'

so we need to use timeout to getting work on page load.

example:

setTimeout(function() {
    var rsul = google.maps.geometry.poly.containsLocation(new google.maps.LatLng(-12.043333, -77.028333), bermudaTriangle);
    console.log(rsul);
}, 1000);

so it is giving proper boolean value

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论