react-native-maps: Custom marker in google map causes high CPU load on iOS
Even if there is only one custom marker in google map, the app uses a LOT of CPU on iOS. It happens on both simulator and device with debug/release builds.
You can see in the screenshot of the demo project I made, when we use custom marker, the CPU usage jumps from 0.8% to 62%. I also tried creating a custom marker in a native iOS app using Google Maps SDK for iOS. It does not use much CPU.
The following two ways of defining a custom marker do not make any difference. Both of them use a lot of CPU.
<MapView.Marker coordinate={coordinate} anchor={{x: 0.65, y: 1}}>
<Image source={imageSrc}/>
</MapView.Marker>
<MapView.Marker coordinate={coordinate} anchor={{x: 0.65, y: 1}} image={imageSrc}/>

I tried to use Instruments to profile the app. For unknown reason, something (the custom marker i guess) keeps redrawing.

Environment: react-native-maps: 0.13.0 react-native: ^0.40.0 map provider: google OS: iOS
Another person encountered the same issue. https://github.com/airbnb/react-native-maps/issues/741#issuecomment-263400988
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 39
- Comments: 61 (15 by maintainers)
Commits related to this issue
- Enable control of Google Maps Marker tracksViewChanges property. Fixes issue #1031, enabling use of custom marker images and views in Google Maps on iOS and android. As detailed in the Google Maps M... — committed to heysailor/react-native-maps by heysailor 7 years ago
- Enable control of Google Maps Marker tracksViewChanges property. (#1705) Fixes issue #1031, enabling use of custom marker images and views in Google Maps on iOS and android. As detailed in the Go... — committed to react-native-maps/react-native-maps by heysailor 7 years ago
FYI noticed that disabling
tracksViewChangeson the_realMarkerinAIRGoogleMapMarker.mseems to resolve the CPU. It adds some rendering quirks for our implementation, so still working on a complete solution. Wanted to throw it out there in case someone with more expertise can add to it.Setting
tracksViewChangeto false works for static markers but requires a lot of workarounds for dynamic re-rendering of markers, so it cannot be considered as ‘official’ fix…I’ve opened an issue on Google’s issue tracker for this: https://issuetracker.google.com/issues/111711319
For version “0.24.2”, we can use prop “icon” instead of “image” to fix the issue
Hi there, so setting
tracksViewChangestofalse, doesn’t render some of my markers, how can I managetracksViewChangesproperly ??Setting
tracksViewChangesto false statically seems to cause a race condition in my app, where sometimes (~20% of instances) only a fraction of the markers render.I’m not sure on the best way, but here is what I do. I have a marker Component that renders <MapView.Marker> with
tracksViewChangesset to true.On
ComponentDidUpdateI set a timeout of 400ms and settracksViewChangesto false.I set
trackViewChangesagain to true when the component is receiving props. Once the component rerenders thecomponentDidUpdateis triggered again and after 400ms thetracksviewChangesis set to false again.This works for me. If anyone knows a better way… please share.
If it helps, the high CPU issue also occurs when using a
MapView.Markerwith an image set.My steps:
Also:
But I cannot use icon prop because I really need to set different width/height depends on business logic.
I have: react-native: 0.61.2 react-native-maps: 0.27.1
@aksonov - can you share some code? In our case
tracksViewChangesset to false after 0.4s seems to do miracles and our map only uses high cpu during the initial rendering of the markers.I also noticed that even if you have a single marker on the map - if
trackViewChangesis set to true, then the CPU is high… It seems that it doesn’t matter if you have 1 or 100 markers - iftrackViewChangesis set to true, then you are going to have high CPU usage.I fixed this by exposing the google maps icon marker property to RN (using the patch from #1491). In the majority of use cases (i.e. you just want a custom marker image) this is sufficient, and much simpler to boot.
I also think that it’s not really nice to have the app consume 100% CPU for some fraction of a second, whether that’s a hard timeout or the delay between componentDidMount and onMapReady firing…
@compojoom Works well, thanks! But I’m not sure it is good fix and looks like react-native-maps Marker could do this (setting tracksViewChanges true/false depending from update state) by itself
https://developers.google.com/maps/documentation/ios-sdk/marker#customize_the_marker_image
Whats the difference between an icon and an image?
Answer: image prop in Marker is an icon, Image is an image.
This occurs when component is not updating and after the map has loaded in.
I’m running on an iPhone 6 simulator.
MapView.MarkerMapView.MarkerGoogle is the better option (and styles!) but the CPU issue makes it unusable 😦
This is my Marker
stinju’s fix worked