2016-10-19 71 views

回答

1

地圖準備就緒後,你可以申請風格變化OnMapReady

@Override 
public void onMapReady(GoogleMap map) { 
    mMap = map; 
    setMapStyle(); 
} 

private void setMapStyle() { 
    MapStyleOptions style = new MapStyleOptions("[" + 
      " {" + 
      " \"featureType\":\"poi.business\"," + 
      " \"elementType\":\"all\"," + 
      " \"stylers\":[" + 
      "  {" + 
      "  \"visibility\":\"off\"" + 
      "  }" + 
      " ]" + 
      " }," + 
      " {" + 
      " \"featureType\":\"transit\"," + 
      " \"elementType\":\"all\"," + 
      " \"stylers\":[" + 
      "  {" + 
      "  \"visibility\":\"off\"" + 
      "  }" + 
      " ]" + 
      " }" + 
      "]"); 

    mMap.setMapStyle(style); 
} 

檢查這些鏈接:MapStyleOptionsGoogleSamples

2

添加自定義樣式,以谷歌地圖是很容易的。檢查下面的代碼。

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

    private GoogleMap mMap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
    } 


    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 
     try { 
      // Customise the styling of the base map using a JSON object defined 
      // in a raw resource file. 
      boolean success = mMap.setMapStyle(
        MapStyleOptions.loadRawResourceStyle(
          MapsActivity.this, R.raw.style_json)); 

      if (!success) { 
       Log.e("Map", "Style parsing failed."); 
      } 
     } catch (Resources.NotFoundException e) { 
      Log.e("Map", "Can't find style.", e); 
     } 
    } 
} 

在res /文件夾下創建一個文件夾名稱raw。將json從google maps api樣式嚮導複製並粘貼到style_json文件並將其添加到原始文件夾中。而已。風格將被應用。檢查這example

1

內部創建res/一個名爲raw的目錄。在原始創建文件name.json並把JSON從Google Maps APIs Styling Wizard裏面

onMapReady(GoogleMap的GoogleMap的)方法把這些代碼

googleMap.setMapStyle(
       MapStyleOptions.loadRawResourceStyle(
         this, R.raw.name.json)); 

,它是所有:)