设置 google 地图的样式

Styling a google map

我想创建一个使用 google 地图的 android 应用程序。我使用了 Google Maps APIs Styling Wizard 并创建了 json 文件。我必须在我的应用程序代码 (android studio) 中进行哪些更改才能应用更改?

地图准备就绪后,您可以在 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);
}

检查这些链接:MapStyleOptions, GoogleSamples

向 google 地图添加自定义样式非常简单。检查下面的代码。

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);
        }
    }
}

在res/文件夹下创建一个名为raw的文件夹。将 google 地图 api 样式向导中的 json 复制并粘贴到 style_json 文件,并将其添加到原始文件夹。而已。将应用样式。检查这个 example.

res/ 中创建一个名为 raw 的目录。在原始文件中,您创建一个文件 name.json 并将 Google Maps APIs Styling Wizard 中的 json 放入其中

onMapReady(GoogleMap googleMap) 方法中放入该代码

googleMap.setMapStyle(
                MapStyleOptions.loadRawResourceStyle(
                        this, R.raw.name.json));

这就是全部:)