Xamarin.Forms.GoogleMaps icon indicating copy to clipboard operation
Xamarin.Forms.GoogleMaps copied to clipboard

Problem when many Pins to add (5.000 in my case)

Open mcferdev opened this issue 6 years ago • 18 comments

VERSIONS

  • Xamarin.Forms.GoogleMaps - 3.0.3
  • Xamarin.Forms - 3.2.0
  • Xamarin.GooglePlayServices.Maps - 60.1142.1

PLATFORMS

  • [x] Android
  • [ ] iOS
  • [ ] UWP

ACTUAL BEHAVIOR

When the list of pins is too long, the app lock. For instance, I have a Collection of Pins "A" that has 500 elements on list... works normally. Another Collection of Pins "B" has almost 5.000 pins. The app lock when start to Add the pins. Nothing happens. Any exception or error...just frozen.

EXPECTED BEHAVIOR

All the pins on the map.

HOW TO REPRODUCE

  1. Create a custom Pin
Pin pinOndeTemTiro = new Pin()
        {
            Type = PinType.Place,
            Icon = BitmapDescriptorFactory.FromBundle("ondetemtiro.png")
        };
  1. Then, I have a Api Call that returns a Json, that is a List<MyModel> .
  2. I get this List and try to populate on the Map.
 mylist = t.Result;

                        for (int index = 0; index < mylist.Count; index++)
                        {
                            try
                            {
                                var item = mylist[index];
                                var obj = item.latlng;
                                var LATLONG = JsonConvert.DeserializeObject<LatLong>(obj);
                                mylist[index].Latlong = LATLONG;

                                pinOndeTemTiro.Label = mylist[index].DsObjeto;
                                pinOndeTemTiro.Address = mylist[index].NmInformacao;
                                pinOndeTemTiro.Position = new Position(mylist[index].Latlong.Lat, mylist[index].Latlong.Lng);

                                map.Pins.Add(pinOndeTemTiro);

                            }
                            catch (Exception ex)
                            {

                            }
                        }

But, how I told, when has 500 items works nicelly. But when has almost 5.000 , locks and nothing happens.

mcferdev avatar Oct 03 '18 18:10 mcferdev

I guess you should reuse your BitmapDescriptor instead of creating the same one for each pin. Also, in your loop you should create a new instance of Pin on every iteration.

itoledo avatar Oct 12 '18 15:10 itoledo

I guess you should reuse your BitmapDescriptor instead of creating the same one for each pin. Also, in your loop you should create a new instance of Pin on every iteration.

Serious? Don´t you think will be slower ?

mcferdev avatar Oct 12 '18 19:10 mcferdev

Well, adding the same object to a collection many times is wrong, unless you want to add the exact same instance of the object (same labels, position, …) to the Pins collection?

itoledo avatar Oct 12 '18 20:10 itoledo

Please check out the issue #497 . And our sample app's code.

https://github.com/amay077/Xamarin.Forms.GoogleMaps/blob/4ab24797821e19768ebf89baa5c6896d4977f0af/XFGoogleMapSample/Droid/CachingNativeBitmapDescriptorFactory.cs

https://github.com/amay077/Xamarin.Forms.GoogleMaps/blob/4ab24797821e19768ebf89baa5c6896d4977f0af/XFGoogleMapSample/Droid/MainActivity.cs

amay077 avatar Oct 17 '18 09:10 amay077

Well, adding the same object to a collection many times is wrong, unless you want to add the exact same instance of the object (same labels, position, …) to the Pins collection?

Well, but thats the point. If the Image and others parameters are the same, why create another collection ? I am creating a collection of Pins A that has differents labels and positions, but same Image A ...then a collection of Pins B with different labels and positions, but same image B... and continuous

 Pin pinApreensaoFuzil = new Pin()
        {
            Tag = "fuzil",
            Type = PinType.Place,
            Label = "Apreensão Fuzil",
            Icon = BitmapDescriptorFactory.FromBundle("apreensaofuzil.png")
        };

mcferdev avatar Oct 22 '18 13:10 mcferdev

Please check out the issue #497 . And our sample app's code.

https://github.com/amay077/Xamarin.Forms.GoogleMaps/blob/4ab24797821e19768ebf89baa5c6896d4977f0af/XFGoogleMapSample/Droid/CachingNativeBitmapDescriptorFactory.cs

https://github.com/amay077/Xamarin.Forms.GoogleMaps/blob/4ab24797821e19768ebf89baa5c6896d4977f0af/XFGoogleMapSample/Droid/MainActivity.cs

I already using it. Doesn´t work .

mcferdev avatar Oct 22 '18 13:10 mcferdev

Did you find a solution?

jvivas avatar Oct 30 '18 22:10 jvivas

Did you find a solution?

No.

mcferdev avatar Nov 26 '18 12:11 mcferdev

Did you find a solution?

No.

Try to use pin aggregation depended on altitude(zoom). Group some near pins to one pin with count label of pins inside it. When you zoom in - groups becomes less and scattered

jocontacter avatar Mar 15 '19 10:03 jocontacter

Did you find a solution?

No.

Try to use pin aggregation depended on altitude(zoom). Group some near pins to one pin with count label of pins inside it. When you zoom in - groups becomes less and scattered

could you write some code as example of doing this explanation? I have the same problem and I want to reduce time of drawing so many pins in map

justwooper avatar Apr 10 '19 14:04 justwooper

i am same trouble did you find good solution @justwooper

BurakOgutken avatar Jul 22 '19 17:07 BurakOgutken

I don't know why you should show 5000+ pin in map/screen? Could you tell me your use case?

amay077 avatar Jul 23 '19 10:07 amay077

Me over 500+ this is vehicle tracking system but we need plate number over the pin @ammay077

BurakOgutken avatar Jul 23 '19 11:07 BurakOgutken

Did you find a solution?

No.

Try to use pin aggregation depended on altitude(zoom). Group some near pins to one pin with count label of pins inside it. When you zoom in - groups becomes less and scattered

could you write some code as example of doing this explanation? I have the same problem and I want to reduce time of drawing so many pins in map

If you don't want to use aggregation, check your caching. This library has some problems with it. Check this, maybe it is your case. But it is about BitmapDescriptorFactory.FromView() laggs. I don't know how good caching optimized with BitmapDescriptorFactory.FromBundle(). Sorry for my bad english

jocontacter avatar Jul 25 '19 12:07 jocontacter

Hello i solved my problem with SkiaSharp i tested 1000 pins without any performans issue..

but i dont understand why android has this solution xamarin forms doesnt have

BurakOgutken avatar Jul 29 '19 14:07 BurakOgutken

Hello i solved my problem with SkiaSharp i tested 1000 pins without any performans issue..

but i dont understand why android has this solution xamarin forms doesnt have

how did skia helped to you? Can you show piece of code?

jocontacter avatar Jul 30 '19 11:07 jocontacter

Hey @BurakOgutken could you please share your solution with us?

dejanbasic avatar Apr 05 '20 19:04 dejanbasic

I'm trying to do this with a BitmapDescriptorFactory but I don't think that the factory itself is the problem in my case.

It seems that adding the pin to the map is a heavy, thread-blocking process on Android. On iOS it performs fine. Maybe the MainThread is held for code that it doesn't need to be held for?

LittleBoxOfChicken avatar Feb 19 '21 13:02 LittleBoxOfChicken