造型谷歌地图

时间:2016-10-19 18:56:59

标签: android google-maps styling

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

3 个答案:

答案 0 :(得分:3)

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

检查以下链接:MapStyleOptionsGoogleSamples

答案 1 :(得分:3)

向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 /文件夹下创建一个原始文件夹名称。将json从google maps api样式向导复制并粘贴到style_json文件,并将其添加到raw文件夹。而已。风格将适用。请检查此example

答案 2 :(得分:2)

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

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

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

并且全部是:)