Bug 38707 - [Android] Conflict in usage of GoogleMap.SetOnCameraChangeListener between GoogleMap.CameraChange (event handler) and MapRenderer.OnElementChanged
Summary: [Android] Conflict in usage of GoogleMap.SetOnCameraChangeListener between Go...
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2016-02-12 10:03 UTC by Thibault Durand
Modified: 2016-12-22 17:00 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Sample project based on the "WorkingWithMaps" sample with custom Android MapRenderer (275.25 KB, application/zip)
2016-02-12 10:03 UTC, Thibault Durand

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Thibault Durand 2016-02-12 10:03:28 UTC
Created attachment 15017 [details]
Sample project based on the "WorkingWithMaps" sample with custom Android MapRenderer


Xamarin Forms & Xamarin.Forms.Maps version: v2.0.1
GooglePlayServices: v27.0.0
Xamarin Studio @Mac OS X 10.11.2

1. When creating a custom MapRenderer with Android
2. In the custom MapRenderer, when subscribing to CameraChange events via GoogleMap.CameraChange
3. Map.VisibleRegion is always null and the renderer callback OnElementPropertyChanged never called any more when user scrolls/pans the map. 

Attached sample project
The attached sample project is based on the WorkingWithMaps sample available at https://github.com/xamarin/xamarin-forms-samples
I created a CustomMap : Map object in the core project and a corresponding CustomAndroidMapRenderer in the Android project. 
CustomAndroidMapRenderer is working as should be but you can uncomment the line at CustomAndroidMapRenderer.cs:70 to created the issue. 
Compare the console output to see that OnElementPropertyChanged is not called for VisibleRegion (because VisibleRegion remains null) if you uncomment line CustomAndroidMapRenderer.cs:70 

What I suspect the problem is
GoogleMap (namespace Android.Gms.Maps) object is defined in NuGet package Xamarin.GooglePlayServices.Maps (v27.0.0)
MapRenderer (namespace Xamarin.Forms.Maps.Android) is defined in NuGet package Xamarin.Forms.Maps.Android (v2.0.1.6505)

The GoogleMap object exposes the method:
public void SetOnCameraChangeListener(GoogleMap.IOnCameraChangeListener listener);
Which allows to set ONE camera change listener. 

But the GoogleMap object also uses this property when registering custom even handlers via the GoogleMap.CameraChange event handler. 

MapRenderer.OnElementChanged uses SetOnCameraChangeListener to register for camera change events but as soon as someone else (say, me) uses GoogleMap.CameraChange to register for camera change events, it changes GoogleMap's OnCameraChangeListener to be able to catch de CameraChangeEvents, thus removing the CameraChangeListener that the MapRenderer did previously set. 
Therefore, MapRenderer does not get the callback any more and cannot update Map.VisibleRegion nor trigger the OnElementPropertyChanged event. 

What about iOS?

iOS has a similar mechanism with MKMapView.RegionChanged event handler but is NOT affected by a similar issue. 
OnElementPropertyChanged is called for VisibleRegion whether you subscribe to RegionChanged or not.
Comment 1 Thibault Durand 2016-02-12 10:06:23 UTC
I'm rather uncertain I did post this bug in the right section because I couldn't decide whether the bug belongs to the binding of Android.Gms.Maps.GoogleMap or to the use Xamarin.Forms.Maps.Android.MapRenderer makes of it. 

Please tell me if you wish me to report it to the Xamarin.Forms section instead. 

Comment 2 Paul DiPietro [MSFT] 2016-12-22 17:00:03 UTC
This looks to no longer be occurring with the latest stable versions of Forms/Maps.