我正在寻找一种在 MapView for Android 中更改道路颜色(黄色)的方法。我想过重写该onDraw方法,然后遍历每个像素并更改它,但该方法是最终的。
我还想过用 ViewGroup 包装 MapView,然后尝试覆盖它的 onDraw,但我不知道该怎么做。
有人有想法吗?
谢谢。
我正在寻找一种在 MapView for Android 中更改道路颜色(黄色)的方法。我想过重写该onDraw方法,然后遍历每个像素并更改它,但该方法是最终的。
我还想过用 ViewGroup 包装 MapView,然后尝试覆盖它的 onDraw,但我不知道该怎么做。
有人有想法吗?
谢谢。
现在(我不知道从什么时候开始,从 Google Maps API V3.0 看来)它很容易使用Maps Android API 的Styled Map功能。对于地图样式 JSON 准备,您可以使用Styling Withard。您也可以只将必要的部分添加到 JSON 样式对象,而不是所有地图元素。例如,对于黄色道路(带有蓝色标签),JSON ( /res/raw/map_style.json) 可以是:
[
{
"featureType": "road",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#ffff00"
}
]
},
{
"featureType": "road",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#0000ff"
}
]
}
]
和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>
结果你应该得到:
您还可以为静态地图添加样式参数:对于
要求你得到:
更多详情请查看官方博客。
我建议您考虑使用 OpenStreetMap 数据而不是 Google MapView 切换到osmdroid,并修改街道颜色渲染的源代码。