Skip to content

Commit

Permalink
[android] Add customMapStyle prop support to MapView for android (rea…
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-alamine authored and Exilz committed Dec 9, 2016
1 parent 5a06aaf commit 48c82fe
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 1 deletion.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,24 @@ For Android: add the following line in your AndroidManifest.xml
```
For IOS: configure [App Transport Security](https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW33) in your app

### Customizing the map style

Create the json object, or download a generated one from the [google style generator](https://mapstyle.withgoogle.com/).

```jsx
// The generated json object
mapStyle = [ ... ]

render() {
return (
<MapView
region={this.state.region}
onRegionChange={this.onRegionChange}
customMapStyle={mapStyle}
/>
);
}
```

## Examples

Expand Down Expand Up @@ -201,6 +218,11 @@ API could.
![](http://i.giphy.com/3o6UB7poyB6YJ0KPWU.gif) ![](http://i.giphy.com/xT77Yc4wK3pzZusEbm.gif)


### Changing the style of the map

![](http://i.imgur.com/a9WqCL6.png)



### Arbitrary React Views as Markers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MapStyleOptions;

import java.util.Map;

Expand Down Expand Up @@ -92,6 +93,11 @@ public void setMapType(AirMapView view, @Nullable String mapType) {
int typeId = MAP_TYPES.get(mapType);
view.map.setMapType(typeId);
}

@ReactProp(name = "customMapStyleString")
public void setMapStyle(AirMapView view, @Nullable String customMapStyleString) {
view.map.setMapStyle(new MapStyleOptions(customMapStyleString));
}

@ReactProp(name = "showsUserLocation", defaultBoolean = false)
public void setShowsUserLocation(AirMapView view, boolean showUserLocation) {
Expand Down
20 changes: 19 additions & 1 deletion components/MapView.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ const propTypes = {
*/
style: View.propTypes.style,

/**
* A json object that describes the style of the map. This is transformed to a string
* and saved in mayStyleString to be sent to android and ios
* https://developers.google.com/maps/documentation/ios-sdk/styling#use_a_string_resource
* https://developers.google.com/maps/documentation/android-api/styling
*/
customMapStyle: PropTypes.array,

/**
* A json string that describes the style of the map
* https://developers.google.com/maps/documentation/ios-sdk/styling#use_a_string_resource
* https://developers.google.com/maps/documentation/android-api/styling
*/
customMapStyleString: PropTypes.string,

/**
* If `true` the app will ask for the user's location.
* Default value is `false`.
Expand Down Expand Up @@ -386,12 +401,15 @@ class MapView extends React.Component {
}

_onMapReady() {
const { region, initialRegion } = this.props;
const { region, initialRegion, customMapStyle } = this.props;
if (region) {
this.map.setNativeProps({ region });
} else if (initialRegion) {
this.map.setNativeProps({ region: initialRegion });
}
if (customMapStyle) {
this.map.setNativeProps({ customMapStyleString: JSON.stringify(customMapStyle) });
}
this.setState({ isReady: true });
}

Expand Down
2 changes: 2 additions & 0 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import LiteMapView from './examples/LiteMapView';
import CustomTiles from './examples/CustomTiles';
import ZIndexMarkers from './examples/ZIndexMarkers';
import StaticMap from './examples/StaticMap';
import MapStyle from './examples/MapStyle';

const IOS = Platform.OS === 'ios';
const ANDROID = Platform.OS === 'android';
Expand Down Expand Up @@ -140,6 +141,7 @@ class App extends React.Component {
[LiteMapView, 'Android Lite MapView'],
[CustomTiles, 'Custom Tiles', true],
[ZIndexMarkers, 'Position Markers with Z-index', true],
[MapStyle, 'Customize the style of the map', true],
]
// Filter out examples that are not yet supported for Google Maps on iOS.
.filter(example => ANDROID || (IOS && (example[2] || !this.state.useGoogleMaps)))
Expand Down
222 changes: 222 additions & 0 deletions example/examples/MapStyle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
import React from 'react';
import {
StyleSheet,
View,
Dimensions,
} from 'react-native';

import MapView from 'react-native-maps';

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;

const customStyle = [
{
elementType: 'geometry',
stylers: [
{
color: '#242f3e',
},
],
},
{
elementType: 'labels.text.fill',
stylers: [
{
color: '#746855',
},
],
},
{
elementType: 'labels.text.stroke',
stylers: [
{
color: '#242f3e',
},
],
},
{
featureType: 'administrative.locality',
elementType: 'labels.text.fill',
stylers: [
{
color: '#d59563',
},
],
},
{
featureType: 'poi',
elementType: 'labels.text.fill',
stylers: [
{
color: '#d59563',
},
],
},
{
featureType: 'poi.park',
elementType: 'geometry',
stylers: [
{
color: '#263c3f',
},
],
},
{
featureType: 'poi.park',
elementType: 'labels.text.fill',
stylers: [
{
color: '#6b9a76',
},
],
},
{
featureType: 'road',
elementType: 'geometry',
stylers: [
{
color: '#38414e',
},
],
},
{
featureType: 'road',
elementType: 'geometry.stroke',
stylers: [
{
color: '#212a37',
},
],
},
{
featureType: 'road',
elementType: 'labels.text.fill',
stylers: [
{
color: '#9ca5b3',
},
],
},
{
featureType: 'road.highway',
elementType: 'geometry',
stylers: [
{
color: '#746855',
},
],
},
{
featureType: 'road.highway',
elementType: 'geometry.stroke',
stylers: [
{
color: '#1f2835',
},
],
},
{
featureType: 'road.highway',
elementType: 'labels.text.fill',
stylers: [
{
color: '#f3d19c',
},
],
},
{
featureType: 'transit',
elementType: 'geometry',
stylers: [
{
color: '#2f3948',
},
],
},
{
featureType: 'transit.station',
elementType: 'labels.text.fill',
stylers: [
{
color: '#d59563',
},
],
},
{
featureType: 'water',
elementType: 'geometry',
stylers: [
{
color: '#17263c',
},
],
},
{
featureType: 'water',
elementType: 'labels.text.fill',
stylers: [
{
color: '#515c6d',
},
],
},
{
featureType: 'water',
elementType: 'labels.text.stroke',
stylers: [
{
color: '#17263c',
},
],
},
];

class MapStyle extends React.Component {
constructor(props) {
super(props);

this.state = {
};
}

render() {
return (
<View style={styles.container}>
<MapView
provider={this.props.provider}
style={styles.map}
initialRegion={{
latitude: LATITUDE,
longitude: LONGITUDE,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA,
}}
customMapStyle={customStyle}
/>
</View>
);
}
}

MapStyle.propTypes = {
provider: MapView.ProviderPropType,
};

const styles = StyleSheet.create({
container: {
...StyleSheet.absoluteFillObject,
justifyContent: 'flex-end',
alignItems: 'center',
},
map: {
...StyleSheet.absoluteFillObject,
},
});

module.exports = MapStyle;

0 comments on commit 48c82fe

Please sign in to comment.