Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Markers does not update after props change on Android #1611

Closed
levelasquez opened this issue Sep 1, 2017 · 19 comments
Closed

Custom Markers does not update after props change on Android #1611

levelasquez opened this issue Sep 1, 2017 · 19 comments
Labels
bug Something isn't working stale

Comments

@levelasquez
Copy link

Hi everyone,

I'm having an issue on Android where the custom marker does not update after its props have change.

When i press the marker, its marked like selected and vice versa. Then its backgroundColor changes to identify that it has beed selected or not. This is working fine on iOS - Mapkit but on Android - Google Map does not update the change.

On Android - Google Map. Does not update the change.

android

On iOS - Mapkit works perfectly. This is the expected behavior.

ios

Here is my code.

State and method

state = {
    selected: [],
  }

toggleSelect(id) {
    this.state.selected.indexOf(id) > -1
      ? this.setState({ selected: this.state.selected.filter(x => x !== id) })
      : this.setState({ selected: this.state.selected.concat(id) })
  }

MapView Component

<MapView
                style={styles.contentTabView}
                initialRegion={{
                  latitude: -33.447984,
                  longitude: -70.659059,
                  latitudeDelta: 0.0922,
                  longitudeDelta: 0.0421,
                }}
              >
                {data.map(x =>
                  <MapView.Marker
                    key={x._id}
                    coordinate={{
                      longitude: x.location.coordinates[0],
                      latitude: x.location.coordinates[1],
                    }}
                    title={x.name}
                    centerOffset={{ x: 0, y: -20 }}
                    onPress={() => !(x.origin || x.destination) && this.toggleSelect(x._id)}
                  >

                    <Pin
                      pinStyle='pinIcon'
                      sourceIcon={x.clasification.icon}
                      id={x._id}
                      selected={selected}
                      origin={x.origin}
                      destination={x.destination}
                      color={x.timePeriod.color}
                    />
                  </MapView.Marker>
                )}
              </MapView>

Custom Marker Component

<View style={styles[pinStyle].container}>
        <View style={[
          styles[pinStyle].wrapper,
          wrapperColor,
          isSelected(id, selected) && styles[pinStyle].pointSelected,
          (origin || destination) && styles[pinStyle].pointSelected,
        ]}>
          <View style={styles[pinStyle].inner}>
            {type === 'icon'
              ? <IconAwesome
                style={styles[pinStyle].icon}
                name={sourceIcon ? sourceIcon : 'plane'}
              />
              : <Image
                style={styles[pinStyle].image}
                source={{ uri: sourceImg }}
              />}
          </View>
        </View>
      </View>

Helper function

export const isSelected = (_id, selected) => selected.indexOf(_id) > -1 ? true : false

Tell me if you need more details.
Thanks.

@dogcity
Copy link

dogcity commented Sep 4, 2017

There's a solution?

@nschurmann
Copy link

+1, same here!!

@chrismcleod
Copy link

chrismcleod commented Oct 5, 2017

Android seems to be relying only on the key to change in order to update the custom marker. When you re-render try changing your key. This works for me:


<MapView.Marker
  key={ `${marker.id}${Date.now()}` }
  coordinate={ marker }
>
  <CustomMarker marker={marker}>
</MapView.Marker>

@levelasquez
Copy link
Author

levelasquez commented Oct 16, 2017

@chrismcleod This works, not exactly how i expected but works, thanks 👍

This is the code.

key={Platform.select({
    android: selected.includes(x._id || x.provisory_id)
        ? `${x._id || x.provisory_id}${Date.now()}`
        : x._id || x.provisory_id,
    ios: x._id || x.provisory_id,
})}

When i click on the market it re-render how i expected but then it hides the infowindow.

@Stophface
Copy link
Contributor

@chrismcleod livesaver!

@Cosmitar
Copy link

Nice bug :( let's hack everything a little more to find a workaround.

When i click on the market it re-render how i expected but then it hides the infowindow.

I ran into the same issue. Solved this way:

<MapView.Marker
    coordinate={ position }
    key={`${x._id}${isSelected(x._id) ? '-selected' : ''}`} // <-- updates only selected (and deselected) marker
    identifier={x._id} // <-- we need this to invoke showCallout
    ref={(ref) => this._markers[x._id] = ref} // <-- store ref
    onPress={this._onPressMarker}
>
onPressMarker(e) {
  setTimeout(() => // sadly, we need timeout to make this command run in next cycle, after map updates.
    this._markers[e.nativeEvent.id].showCallout()
    , 0);
}

image

@alvelig alvelig added Android bug Something isn't working labels Dec 11, 2017
fqborges added a commit to fqborges/react-native-maps-osmdroid that referenced this issue Jun 11, 2018
fqborges added a commit to fqborges/react-native-maps-osmdroid that referenced this issue Jun 11, 2018
alvelig added a commit that referenced this issue Jun 12, 2018
timxyz pushed a commit to 3sidedcube/react-native-maps that referenced this issue Aug 24, 2018
* master: (168 commits)
  Adding overlaying components details (react-native-maps#2425)
  docs: pin color limitations for android (react-native-maps#2429)
  Revert "Added MBTiles support for iOS and Android (react-native-maps#2208)" (react-native-maps#2387)
  Added MBTiles support for iOS and Android (react-native-maps#2208)
  Fix disabling the toolbar and my location button (react-native-maps#2317)
  Fixes warnings about self (react-native-maps#2341)
  Android: Fix lineCap of Polyline (react-native-maps#2375)
  Update installation.md (react-native-maps#2381)
  update doc (react-native-maps#2363)
  zIndex doesn't work when the map moves in iOS 11 (react-native-maps#2359)
  Fix readme formatting (react-native-maps#2358)
  add support for calloutAnchor with GoogleMaps on iOS; fixes react-native-maps#1852 (react-native-maps#2351)
  Added animateToNavigation method to MapView (react-native-maps#2049)
  Add react-native@^0.55 to peerDependencies (react-native-maps#2332)
  Fix custom marker updates on android react-native-maps#1611 react-native-maps#2048
  [iOS] Prefix or eliminate globals in AIRMapMarker (react-native-maps#2306)
  Fix CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF warnings (react-native-maps#2154)
  Fix for compile error (react-native-maps#2215). (react-native-maps#2232)
  Make tiles display at the same physical size regardless of pixel dens… (react-native-maps#2248)
  Added support of lineDashPattern polyline prop to iOS Google Maps (react-native-maps#2243)
  ...

# Conflicts:
#	lib/components/MapMarker.js
@khuongdv
Copy link

khuongdv commented Sep 6, 2018

Hi there. Any stable solution for this?
So far I did try these:

  • Set dynamic key to Markers like @chrismcleod did
  • Create custom pin (according to some guy in other issue)

But I still got issue that markers don't show on first load.

@zigakerec
Copy link

@khuongdv I have a similar problem with pins not rendering on the first load, did you found any solutions regarding this?

@nikodemrafalski
Copy link

To me, not setting key prop of MapView.Marker solved disappearing markers.
Just pass an id prop (docs list it). No `Math.random() or Date() magic, just some meaningful identifier.

@stianjensen
Copy link

Just pass an id prop (docs list it).

Do you mean the property called identifier, listed in the table here?

@nikodemrafalski
Copy link

Ok, probably my suggestion isn't worth anything, I was referring to https://github.com/react-community/react-native-maps/blob/master/docs/mapview.md where Marker type is defined.
So probably the fact I'm providing id isn't changing anything. But still I decided not to use key and for now, it solved issued in my app

@DerekPark
Copy link

I used the Date.now() as suggested by @chrismcleod in key={ ${marker.id}${Date.now()} }, but I found it caused a huge performance loss, instead I used the value the marker update depends on to change the key, and that worked well. In my case I wanted to update marker colors so I used: key={ ${key}${marker[key].color} }

dmitriismitnov added a commit to 91team/react-native-maps that referenced this issue Dec 5, 2018
@dmitryshelomanov
Copy link

@DerekPark i used similar solution. Uniq id + isActive state

@Balasnest
Copy link

Balasnest commented Sep 15, 2019

Date.now() cause a huge performance loss. It re-creates the marker instead of re-render every time.

@zaidraddad94
Copy link

      tracksViewChanges={true}

use this in the marker

@dmitryshelomanov
Copy link

      tracksViewChanges={true}

use this in the marker

For android it does not work
It seems

@stale
Copy link

stale bot commented Oct 30, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Oct 30, 2020
@stale stale bot closed this as completed Nov 6, 2020
@shehrozsheikh
Copy link

Android seems to be relying only on the key to change in order to update the custom marker. When you re-render try changing your key. This works for me:


<MapView.Marker
  key={ `${marker.id}${Date.now()}` }
  coordinate={ marker }
>
  <CustomMarker marker={marker}>
</MapView.Marker>

@chrismcleod you're a hero, man!!

@moneszarrugh
Copy link

Strangely this issue is happening to me on ios (google maps) but not on android. Changing the key did fix it for me though.

pinpong pushed a commit to pinpong/react-native-maps that referenced this issue Feb 28, 2025
pinpong pushed a commit to pinpong/react-native-maps that referenced this issue Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stale
Projects
None yet
Development

No branches or pull requests