Reputation: 29
I want to create an android app that uses google maps. I used the Google Maps APIs Styling Wizard and created the json file. What changes do I have to make in my app's code(android studio) in order to apply changes?
Upvotes: 1
Views: 4154
Reputation: 125
create inside res/ a directory called raw. in raw you create a file name.json and put the json from Google Maps APIs Styling Wizard inside it
in the onMapReady(GoogleMap googleMap) method put that code
googleMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(
this, R.raw.name.json));
and it is all :)
Upvotes: 3
Reputation: 2034
Adding a custom style to google map is very easy. Check the below code.
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
try {
// Customise the styling of the base map using a JSON object defined
// in a raw resource file.
boolean success = mMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(
MapsActivity.this, R.raw.style_json));
if (!success) {
Log.e("Map", "Style parsing failed.");
}
} catch (Resources.NotFoundException e) {
Log.e("Map", "Can't find style.", e);
}
}
}
Create a folder name raw under res/ folder. Copy and paste the json from the google maps api styling wizard to a style_json file and add it to the raw folder. Thats it. Style will be applied. Check this example.
Upvotes: 3
Reputation: 5062
After map ready, you could apply style changes in OnMapReady
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
setMapStyle();
}
private void setMapStyle() {
MapStyleOptions style = new MapStyleOptions("[" +
" {" +
" \"featureType\":\"poi.business\"," +
" \"elementType\":\"all\"," +
" \"stylers\":[" +
" {" +
" \"visibility\":\"off\"" +
" }" +
" ]" +
" }," +
" {" +
" \"featureType\":\"transit\"," +
" \"elementType\":\"all\"," +
" \"stylers\":[" +
" {" +
" \"visibility\":\"off\"" +
" }" +
" ]" +
" }" +
"]");
mMap.setMapStyle(style);
}
Check these links : MapStyleOptions, GoogleSamples
Upvotes: 3