简体   繁体   中英

Google maps LatLng and LatLngBounds returning different results

I'm currently learning to use google maps and am following the following to set a max bound for my map: Google Maps v3 - limit viewable area and zoom level

My bounds are set as:

var strictBounds = new google.maps.LatLngBounds(
  new google.maps.LatLng(41.600, -87.857),
  new google.maps.LatLng(42.065, -87.310)
)

Within my dragend event listener, I console.log(strictBounds) and am getting the following:

Google Maps strictBounds 返回

It is returning my south-west Latitude and north-east Latitude together as one LatLng and my south-west Longitude and north-east Longitude as one LatLng .

I could sorta hack it and set south-west and north-east coordinates so that the return would be what I want it to be, but I was wondering why this is happening in this scenario and would I be running into this problem further with other google map methods?

EDIT:

Just to clarify, my two LatLng positions set in strictBounds should be my southWest and northEast positions for the max bounds of the map.

When I use the addListener method provided in the link above (Google Maps v3 - limit viewable area and zoom level), it grabs the wrong lat/long from strictBounds .

Instead of grabbing: (41.6, -87.857), (42.065, -87.310)

It grabs: (41.6, 42.065), (-87.31, -87.856999..)

The way you define the bound is correct. Just don't use console.log(strictBounds). Use the following instead

var sw = strictBounds.getSouthWest();
var ne = strictBounds.getNorthEast();

console.log(sw.lat() + "," + sw.lng());
console.log(ne.lat() + "," + ne.lng());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM