Android的谷歌地图设置样式地图使用JSON
问题描述:
我有开发一个简单的应用程序,我一直在使用这种简单的方式整合了谷歌地图:`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)));
}
,但我不知道如何整合,因为这是样的活动,而不是我用一个片段,也当我打算进军添加以下代码给我一个错误。 有什么帮助吗? 感谢
答
后良好的科研和代码我实现了谷歌地图的片段用下面的代码:
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并相应地设计你的地图。
你在哪里得到错误的代码? –