2016-12-02 95 views
0

我有開發一個簡單的應用程序,我一直在使用這種簡單的方式整合了谷歌地圖:`Android的谷歌地圖設置樣式地圖使用JSON

mapFragment.getMapAsync(new OnMapReadyCallback() { 
     @Override 
     public void onMapReady(GoogleMap googleMap) { 
      mClusterManager = new ClusterManager<Car2GoClusterItem>(getActivity(), map); 

      map.setOnCameraChangeListener(mClusterManager); 
      map.setInfoWindowAdapter(mClusterManager.getMarkerManager()); 
      map.setOnMarkerClickListener(mClusterManager); 


      mClusterManager.setOnClusterItemClickListener(
        new ClusterManager.OnClusterItemClickListener<Car2GoClusterItem>() { 
         @Override 
         public boolean onClusterItemClick(Car2GoClusterItem item) { 
          clickedClusterItem = item; 
          return false; 
         } 
        }); 

    }); 
} 

public GoogleMap.OnCameraChangeListener getCameraChangeListener() { 
    return new GoogleMap.OnCameraChangeListener() { 
     @Override 
     public void onCameraChange(CameraPosition position) { 
      Log.d("Zoom", "Zoom: " + position.zoom); 

      if (previousZoomLevel <= 15 && position.zoom > 15) { 
       map.clear(); 
       processMap(v, 16); 
      } 

      if (previousZoomLevel >= 15 && position.zoom < 15) { 
       map.clear(); 
       processMap(v, 14); 
      } 

      previousZoomLevel = position.zoom; 
     } 
    }; 
} 

public void processMap(View v, int zoom) { 
    if (map == null) { 
     map = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 


    } 

    if (map != null) { 
    //// code here 
    } 
    } 

現在我想爲我的映象的MapStyle通過設置JSON和我看到這個代碼:

public void onMapReady(GoogleMap googleMap) { 

    // Customise the styling of the base map using a JSON object defined 
    // in a string resource file. First create a MapStyleOptions object 
    // from the JSON styles string, then pass this to the setMapStyle 
    // method of the GoogleMap object. 
    boolean success = googleMap.setMapStyle(new MapStyleOptions(getResources() 
      .getString(R.string.style_json))); 

    if (!success) { 
     Log.e(TAG, "Style parsing failed."); 
    } 
    // Position the map's camera near Sydney, Australia. 
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(-34, 151))); 
} 

,但我不知道如何整合,因爲這是樣的活動,而不是我用一個片段,也當我打算進軍添加以下代碼給我一個錯誤。 有什麼幫助嗎? 感謝

+0

你在哪裏得到錯誤的代碼? –

回答

0

後良好的科研和代碼我實現了谷歌地圖的片段用下面的代碼:

import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.ContextMenu; 
import android.view.LayoutInflater; 
import android.view.MenuInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapView; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.LatLngBounds; 
import com.google.android.gms.maps.model.Marker; 
import com.google.android.gms.maps.model.MarkerOptions; 

import java.util.ArrayList; 


public class MapFragment extends Fragment implements OnMapReadyCallback{ 


    public MapFragment() { 
     // Required empty public constructor 
    } 

    private MapView mapView; 
    private GoogleMap googleMap; 
    View rootView; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     rootView = inflater.inflate(R.layout.fragment_map, container, false); 
     SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map); 
     return rootView; 
    } 

    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     mapView = (MapView) view.findViewById(R.id.map); 
     mapView.onCreate(savedInstanceState); 
     mapView.onResume(); 
     mapView.getMapAsync(this); 
    } 

    @Override 
    public void onMapReady(GoogleMap map) { 
     googleMap = map; 

     googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN); 
     googleMap.setMinZoomPreference(12.6f); 
     googleMap.getUiSettings().setAllGesturesEnabled(true); 
    } 
} 

下面是我使用的地圖片段

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <com.google.android.gms.maps.MapView 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</android.support.design.widget.CoordinatorLayout> 

在XML代碼你可以抽出這個片段可以得到JSON並相應地設計你的地圖。