根据我目前所读到的内容,似乎无法通过Android API设置Google地图的样式。
有没有人看到不同的或者知道在Android上设置Google地图样式的方法(更改要素颜色等)?
据我所知,Android(或iOS)完整地图库的唯一替代方案是Mapbox,但他们的Android库仍在大力开发中。
答案 0 :(得分:0)
我们可以在android中创建样式化的地图:
StyledMapDemoActivity.java
public class StyledMapDemoActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap = null;
private static final String TAG = StyledMapDemoActivity.class.getSimpleName();
private static final String SELECTED_STYLE = "selected_style";
// Stores the ID of the currently selected style, so that we can re-apply it when
// the activity restores state, for example when the device changes orientation.
private int mSelectedStyleId = R.string.style_label_default;
// These are simply the string resource IDs for each of the style names. We use them
// as identifiers when choosing which style to apply.
private int mStyleIds[] = {
R.string.style_label_retro,
R.string.style_label_night,
R.string.style_label_grayscale,
R.string.style_label_no_pois_no_transit,
R.string.style_label_default,
};
private static final LatLng SYDNEY = new LatLng(-33.8688, 151.2093);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mSelectedStyleId = savedInstanceState.getInt(SELECTED_STYLE);
}
setContentView(R.layout.styled_map_demo);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onSaveInstanceState(Bundle outState) {
// Store the selected map style, so we can assign it when the activity resumes.
outState.putInt(SELECTED_STYLE, mSelectedStyleId);
super.onSaveInstanceState(outState);
}
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, 14));
setSelectedStyle();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.styled_map, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_style_choose) {
showStylesDialog();
}
return true;
}
/**
* Shows a dialog listing the styles to choose from, and applies the selected
* style when chosen.
*/
private void showStylesDialog() {
// mStyleIds stores each style's resource ID, and we extract the names here, rather
// than using an XML array resource which AlertDialog.Builder.setItems() can also
// accept. We do this since using an array resource would mean we would not have
// constant values we can switch/case on, when choosing which style to apply.
List<String> styleNames = new ArrayList<>();
for (int style : mStyleIds) {
styleNames.add(getString(style));
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.style_choose));
builder.setItems(styleNames.toArray(new CharSequence[styleNames.size()]),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mSelectedStyleId = mStyleIds[which];
String msg = getString(R.string.style_set_to, getString(mSelectedStyleId));
Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
Log.d(TAG, msg);
setSelectedStyle();
}
});
builder.show();
}
/**
* Creates a {@link MapStyleOptions} object via loadRawResourceStyle() (or via the
* constructor with a JSON String), then sets it on the {@link GoogleMap} instance,
* via the setMapStyle() method.
*/
private void setSelectedStyle() {
MapStyleOptions style;
switch (mSelectedStyleId) {
case R.string.style_label_retro:
// Sets the retro style via raw resource JSON.
style = MapStyleOptions.loadRawResourceStyle(this, R.raw.mapstyle_retro);
break;
case R.string.style_label_night:
// Sets the night style via raw resource JSON.
style = MapStyleOptions.loadRawResourceStyle(this, R.raw.mapstyle_night);
break;
case R.string.style_label_grayscale:
// Sets the grayscale style via raw resource JSON.
style = MapStyleOptions.loadRawResourceStyle(this, R.raw.mapstyle_grayscale);
break;
case R.string.style_label_no_pois_no_transit:
// Sets the no POIs or transit style via JSON string.
style = new MapStyleOptions("[" +
" {" +
" \"featureType\":\"poi.business\"," +
" \"elementType\":\"all\"," +
" \"stylers\":[" +
" {" +
" \"visibility\":\"off\"" +
" }" +
" ]" +
" }," +
" {" +
" \"featureType\":\"transit\"," +
" \"elementType\":\"all\"," +
" \"stylers\":[" +
" {" +
" \"visibility\":\"off\"" +
" }" +
" ]" +
" }" +
"]");
break;
case R.string.style_label_default:
// Removes previously set style, by setting it to null.
style = null;
break;
default:
return;
}
mMap.setMapStyle(style);
}
}
StyledMapDemoActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
答案 1 :(得分:0)
Google引入了基于云的地图样式(但是现在处于Beta中)。使用此功能,可以在Google Cloud Platform中设置地图ID和地图样式,并在Android应用中使用它(您在应用中唯一需要做的就是指定地图ID)。>
以下是详细信息: JSESSIONID
PS这是'old'选项的说明,您需要在其中创建一个特殊的json文件并在Android应用中使用它来获取样式化地图: https://developers.google.com/maps/documentation/android-sdk/cloud-based-map-styling