google-maps – Benefits of Normalizing Longitude to [-180, 180) in Google Maps

extentsgoogle maps

In the Google Maps API v2 for Android, LatLng normalizes longitude values to the range -180 degrees inclusive to +180 degrees exclusive. Consequently, attempting to represent the entire world as a LatLngBounds produces a longitude range of -180 to -180, which has zero width and contains no points:

// southwest, northeast
final LatLngBounds world = new LatLngBounds(
    new LatLng(-90, -180),
    new LatLng(90, 180));
assertTrue(world.contains(new LatLng(0, 0))); // test fails

So obviously, normalizing longitude to [-180, 180) has a cost in terms of how LatLngBounds works. We can only speculate as to why the API designers might have made this decision. But what are the potential benefits of normalizing longitude this way? Are there any common operations that are simplified?

Best Answer

+180 and -180 are the same meridian on the Earth, so they are essentially the same value when defining extents. And as you demonstrated, defining extents with the same value will produce zero width extents.

This is why one of the +-180 values must be exclusive and one must be inclusive.

edit: Technically, there isn't a reason why they couldn't have +180 and -180 both be valid, but when creating an API, less is more. It's better to restrict input up front, rather than having to deal with multiple cases for the same value in your code.

This is somewhat similar to using One's Complement to store signed integer values. Having to test for two different representations of zero is a pain, so this is one of the reasons why Two's Complement is much more popular.

Related Question