2012-11-18 106 views
3

我正在尋找一種在MapView for Android中更改道路顏色(黃色)的方法。我想覆寫onDraw方法,然後遍歷每個像素並改變它,但該方法是最終的。如何更改Google地圖中道路的顏色?

我也想過用ViewGroup包裝MapView,然後嘗試覆蓋它的onDraw,但我不知道如何去做。

有沒有人有想法?

謝謝。

回答

0

我建議您考慮使用OpenStreetMap數據而不是Google MapView切換到osmdroid,並修改街道顏色渲染的源代碼。

0

現在(我不知道從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> 

因此,您應該得到:

Android styled map fragment

您還可以添加樣式參數的靜態地圖: 爲

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瞭解更多詳情。

相關問題