3

これまで読んだことから、 Android APIを介して Google マップのスタイルを設定する方法はないようです。

Android で Google マップのスタイルを変更する方法 (機能の色を変更するなど) を別の方法で見たり、知っている人はいますか?

私が見た限りでは、Android (または iOS) 用の完全なマップ ライブラリの唯一の代替手段はMapboxですが、彼らの Android ライブラリはまだ開発中です。

4

2 に答える 2

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" />
于 2016-10-10T05:43:49.780 に答える