android - 如何更改 Google map 中道路的颜色?

标签 android google-maps android-mapview android-canvas

我正在寻找一种在 Android 版 MapView 中更改道路颜色(黄色)的方法。我想重写 onDraw 方法,然后遍历每个像素并更改它,但该方法是最终的。

我还想过用 ViewGroup 包装 MapView,然后尝试覆盖它的 onDraw,但我不确定该怎么做。

有没有人有想法?

谢谢。

最佳答案

现在(我不知道从什么时候开始,从 Google Maps API V3.0 看来)很容易使用 Styled Map Maps Android API 的功能。对于 map 样式 JSON 准备,您可以使用 Styling Withard .您也可以只向 JSON 样式对象添加必要的部分,而不是为所有 map 元素添加。例如,对于黄色道路(带有蓝色标签)JSON (/res/raw/map_style.json) 可以是:

[
  {
    "featureType": "road",
    "elementType": "geometry.fill",
    "stylers": [
      {
        "color": "#ffff00"
      }
    ]
  },
  {
    "featureType": "road",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#0000ff"
      }
    ]
  }

]

和带有 map fragment 的MainActyvity.java:

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

    private GoogleMap mGoogleMap;
    private MapFragment mapFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map_fragment);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;

        try {
            // Customise the styling of the base map using a JSON object defined
            // in a raw resource file.
            boolean success = mGoogleMap.setMapStyle(
                    MapStyleOptions.loadRawResourceStyle(
                            this, R.raw.map_style));

            if (!success) {
                Log.e(TAG, "Style parsing failed.");
            }
        } catch (Resources.NotFoundException e) {
            Log.e(TAG, "Can't find style. Error: ", e);
        }
        // Position the map's camera near Sydney, Australia.
        mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(50.4501,30.5234), 16.0f));

    }

}

activity_main.xls:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.test.just.googlemapsgeneral.activities.MainActivity">

    <fragment
        android:id="@+id/map_fragment"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

结果你应该得到:

Android styled map fragment

静态 map 也可以添加样式参数: 对于

https://maps.googleapis.com/maps/api/staticmap?&key=[your_MAPS_API_KEY]&center=50.4501,30.5234&zoom=16&size=640x640&style=feature:road|element:geometry|color:0xFFFF00

你得到的要求:

Styled static map

请参阅Official Blog了解更多详情。

关于android - 如何更改 Google map 中道路的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13439628/

相关文章:

android - 使用 Google Maps Android API v2 时出错

javascript - 更新热图数据,简单的 google HeatMap

java - 在 Android - Java 中显示自定义样式的 Google map

android - 如何从多个数据库的表进行连接?

google-maps - 如何在 Flutter 中自定义谷歌地图标记图标

java - 自定义接收器无法正确启动

Android mapview 有时会崩溃(幻像 nullpointerexception)

android - IllegalArgumentException:在 Google MapView 的 zoomOut 期间宽度和高度必须 > 0

android - Android 中的 Spring restclient 通过 HTTP GET 检索 XML 数据

Android PagedList 更新