I beginning to use google style wizard, and i was wondering to know if it is possible coloring the streets (example: local street) in different colours by area determined. I would be really gratefull if you could tell me how to do it in android studio 2.2.
1 Answers
You will need to define a json
file to style your map defining the features that you want to style and the stylers (In the example I'm styling local roads with red color).
style_json.json
[
{
"featureType": "road.local",
"elementType": "geometry",
"stylers": [
{
"color": "#ff0000"
}
]
}
]
Then you will need to apply the style to the map:
MapsActivity.java
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(
this, R.raw.style_json));
}
}
Here you can find the style reference.
Take into account that according to the documentation styling works only on the normal
map type.
Also note that this styling applies to the whole map. You can't for example style local roads in red for the USA and in blue for Europe out of the box, but you can emulate this behaviour using an OnCameraIdleListener
and styling the map with different json
files based on the CameraPosition
(target
, zoom
, bearing
, zoom
). For example (styling local roads using R.raw.style_json
if latitude > 40.4
and using R.raw.style_json2
otherwise):
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnCameraIdleListener {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
this.mMap = googleMap;
mMap.setOnCameraIdleListener(this);
LatLng ll1 = new LatLng(40.4, -3.7);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ll1, 17));
}
@Override
public void onCameraIdle() {
if (mMap.getCameraPosition().target.latitude > 40.4) {
mMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(
this, R.raw.style_json));
} else {
mMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(
this, R.raw.style_json2));
}
}
}

- 18,044
- 4
- 45
- 61