我正在尋找一種在MapView for Android中更改道路顏色(黃色)的方法。我想覆寫onDraw
方法,然後遍歷每個像素並改變它,但該方法是最終的。如何更改Google地圖中道路的顏色?
我也想過用ViewGroup包裝MapView,然後嘗試覆蓋它的onDraw,但我不知道如何去做。
有沒有人有想法?
謝謝。
我正在尋找一種在MapView for Android中更改道路顏色(黃色)的方法。我想覆寫onDraw
方法,然後遍歷每個像素並改變它,但該方法是最終的。如何更改Google地圖中道路的顏色?
我也想過用ViewGroup包裝MapView,然後嘗試覆蓋它的onDraw,但我不知道如何去做。
有沒有人有想法?
謝謝。
我建議您考慮使用OpenStreetMap數據而不是Google MapView切換到osmdroid,並修改街道顏色渲染的源代碼。
現在(我不知道從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>
因此,您應該得到:
您還可以添加樣式參數的靜態地圖: 爲
要求你有:
請參閱Official Blog瞭解更多詳情。