admin管理员组

文章数量:1023838

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

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

本文标签: javascriptGoogle Maps API v3googlemapsgeometrypolycontainsLocation not working the first timeStack Overflow