forked from react-native-maps/react-native-maps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Android] Marker share icon image (react-native-maps#2741)
* Add a test example to add massive number of markers on map * Share image for markers to reduce memory usage Use a shared image icon for markers instead of loading and creating bitmap for each marker, which uses more memory in case when we have a lot of markers but only use a limited set of images.
- Loading branch information
1 parent
b7cba7d
commit 723db2c
Showing
7 changed files
with
320 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import React from 'react'; | ||
import { | ||
StyleSheet, | ||
View, | ||
Text, | ||
Dimensions, | ||
TouchableOpacity, | ||
} from 'react-native'; | ||
|
||
import MapView, { Marker, ProviderPropType } from 'react-native-maps'; | ||
import flagPinkImg from './assets/flag-pink.png'; | ||
|
||
const { width, height } = Dimensions.get('window'); | ||
|
||
const ASPECT_RATIO = width / height; | ||
const LATITUDE = 37.78825; | ||
const LONGITUDE = -122.4324; | ||
const LATITUDE_DELTA = 0.0922; | ||
const LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO; | ||
let id = 0; | ||
|
||
class MassiveCustomMarkers extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
region: { | ||
latitude: LATITUDE, | ||
longitude: LONGITUDE, | ||
latitudeDelta: LATITUDE_DELTA, | ||
longitudeDelta: LONGITUDE_DELTA, | ||
}, | ||
markers: [], | ||
}; | ||
|
||
this.onMapPress = this.onMapPress.bind(this); | ||
} | ||
|
||
generateMarkers(fromCoordinate) { | ||
const result = []; | ||
const { latitude, longitude } = fromCoordinate; | ||
for (let i = 0; i < 100; i++) { | ||
const newMarker = { | ||
coordinate: { | ||
latitude: latitude + (0.001 * i), | ||
longitude: longitude + (0.001 * i), | ||
}, | ||
key: `foo${id++}`, | ||
}; | ||
result.push(newMarker); | ||
} | ||
return result; | ||
} | ||
|
||
onMapPress(e) { | ||
this.setState({ | ||
markers: [ | ||
...this.state.markers, | ||
...this.generateMarkers(e.nativeEvent.coordinate), | ||
], | ||
}); | ||
} | ||
|
||
render() { | ||
return ( | ||
<View style={styles.container}> | ||
<MapView | ||
provider={this.props.provider} | ||
style={styles.map} | ||
initialRegion={this.state.region} | ||
onPress={this.onMapPress} | ||
> | ||
{this.state.markers.map(marker => ( | ||
<Marker | ||
title={marker.key} | ||
image={flagPinkImg} | ||
key={marker.key} | ||
coordinate={marker.coordinate} | ||
/> | ||
))} | ||
</MapView> | ||
<View style={styles.buttonContainer}> | ||
<TouchableOpacity | ||
onPress={() => this.setState({ markers: [] })} | ||
style={styles.bubble} | ||
> | ||
<Text>Tap to create 100 markers</Text> | ||
</TouchableOpacity> | ||
</View> | ||
</View> | ||
); | ||
} | ||
} | ||
|
||
MassiveCustomMarkers.propTypes = { | ||
provider: ProviderPropType, | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
...StyleSheet.absoluteFillObject, | ||
justifyContent: 'flex-end', | ||
alignItems: 'center', | ||
}, | ||
map: { | ||
...StyleSheet.absoluteFillObject, | ||
}, | ||
bubble: { | ||
backgroundColor: 'rgba(255,255,255,0.7)', | ||
paddingHorizontal: 18, | ||
paddingVertical: 12, | ||
borderRadius: 20, | ||
}, | ||
latlng: { | ||
width: 200, | ||
alignItems: 'stretch', | ||
}, | ||
button: { | ||
width: 80, | ||
paddingHorizontal: 12, | ||
alignItems: 'center', | ||
marginHorizontal: 10, | ||
}, | ||
buttonContainer: { | ||
flexDirection: 'row', | ||
marginVertical: 20, | ||
backgroundColor: 'transparent', | ||
}, | ||
}); | ||
|
||
export default MassiveCustomMarkers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.