0

First let me state that this question concerns Map v2 API for my native Android application. Second, I've looked at a number of stackOverflow postings on how to detect panning changes on Map v2 API (e.g., How to handle onTouch event for map in Google Map API v2?) but at this point it seems like these solutions are bit of overkill given my requirements.

Here is what I'm trying to accomplish...

Detect if the user has changed the original camera position (zoom or pan) of a map and if so display a reset button on the map so that the user can simply reset the map to the original position by tapping the reset button.

Here is my approach.....

To accomplish this I use the "OnCameraChangeListener getCameraChangeListener()" callback and on callback test whether the camera position passed in (via the onCameraChange(CameraPosition position) is different than the "original" camera position of the rendered map. If it is different then I display the reset button so that the user can return the map to it's position.

Here is the problem that I'm encountering....

Everything works as expected except that when the "OnCameraChange" callback is called in response to resetting the map image to it's "original" position (via the "reset" button) the camera "position" passed in via the "onCameraChange" callback doesn't match the "original" map position (as I would have expected) even though the map has been reset appropriately.

This creates a problem for me because if the Camera position passed in via the callback doesn't match the "original" position then I make the reset button visible. But in this because I have successfully reset the map to it's original position I no longer want the reset button to be visible...hence my problem.

Here is where I need your help....

  • What am I missing here? As stated above I would have expected that the map "position" returned (via the onCameraChange) on the last callback would have matched the "original" camera position...after all the reset to the "original" map position is what triggered that last onCameraChange callback.

Regards.

Community
  • 1
  • 1
user2101068
  • 617
  • 1
  • 8
  • 23

2 Answers2

1

I have just tested that and also reported on gmaps-api-issues.

For now you will have to write your own equals(CameraPosition, CameraPosition), which take into accont target LatLng is different by a very small value.

My test shows this:

CameraPosition{target=lat/lng: (0.2345,12.98231), zoom=5.4231, tilt=13.33, bearing=40.0}
CameraPosition{target=lat/lng: (0.23449985034422136,12.982310056686401), zoom=5.4231, tilt=13.33, bearing=40.0}
MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
  • Thank you...at least now I know that it is a defect. I was pulling out my hair trying to figure out why it wasn't working. I "starred" the issue. Hopefully they will fix it soon. – user2101068 May 07 '13 at 22:16
  • I wouldn't expect it to be fixed **very** soon. Doing a workaround with your own `equals` implementation shouldn't take more than few minutes. Waiting can take like 1 to 3 months ;) – MaciejGórski May 07 '13 at 22:21
  • Yes I guess I could implement the equals method as you described above. I first need to determine how far the onCameraChange positions is off from the actual map position...if it's not too far off then what you suggest seems reasonable...however if the position returned is off by "a lot" then doing what you suggested will not help. Just curious have you done any calculations to determine how far off it is? a few feet? 10's of feet? hundred's of feet? etc. – user2101068 May 07 '13 at 22:23
  • Test in my answer shows it is less than 0.0000002. Assume `Math.abs(...) < 0.00001` and you are safe. – MaciejGórski May 07 '13 at 22:26
  • 1
    Btw my testing shows that position returned by onCamera change is off by just a little. In my very brief testing the Camera Position was off (delta distance) a maximum of ~.00004 yards at a given zoom level. That delta distance could vary based on the zoom level. While this delta distance may be good enough if you are playing horseshoes or hand grenades it's still not correct and forces the developer to put a workaround in place. – user2101068 May 07 '13 at 23:35
0

Looks like the precision is lost because GMaps is casting LatLng values from Double to float somewhere before passing to onCameraChange

This works in my case:

            public boolean equals(LatLng original, LatLng target) {
                return ((float) original.latitude == (float) target.latitude 
                    || (float) original.longitude == (float) target.longitude);
            }