谷歌地图Android API - 自定义地图样式

3
根据我目前所了解的,似乎没有办法通过Android API来为Google Maps设置样式。
有看到过别的方式或者知道如何在Android上为Google Maps设置样式(改变特征颜色等)的吗?
据我所见,Android(或iOS)上替代完整地图库的唯一选择是Mapbox,但是他们的Android库仍处于大量开发中。

截至本文撰写时,根据文档,这是不可能的。如果您确实需要样式化地图,则应考虑使用Google Maps JavaScript API。 - goblin
我没有注意到那个明确指出的底部框,谢谢提醒。 - sean
最终,这个功能已经可以在Android和iOS上使用了。您可以参考以下文章获取更多详细信息:https://googlegeodevelopers.blogspot.com.es/2016/09/custom-map-styling-with-google-maps.html - xomena
可能是使用Google Map API2为Android设置地图样式的重复问题。 - xomena
2个回答

1
Google推出了基于云的地图样式(目前仍处于测试版)。使用此功能,可以在Google Cloud Platform中设置地图ID和地图样式,并在Android应用程序中使用它(您需要在应用程序中指定地图ID)。
以下是详细信息: https://developers.google.com/maps/documentation/android-sdk/cloud-based-map-styling PS 这里是'旧'选项的描述,您需要创建一个特殊的json文件并在Android应用程序中使用它来获取样式化地图: https://developers.google.com/maps/documentation/android-sdk/styling

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" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接