2012-11-18 108 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了解更多详情。

相关问题