Android Google Map使用json设置样式图

时间:2016-12-02 09:32:03

标签: android google-maps

我开发了一个简单的应用程序,并使用这种简单的方式集成了谷歌地图:`

    <div ngf-drop ng-model="files" ngf-pattern="image/*" data-ngf-multiple="true" ng-repeat="image in images1" ngf-size>
        <img  src="{{image}}" width = "100%" height = "100%" class="m-l img-responsive img-rounded m-b" alt="post images">
        </img-crop>
        <span ng-show="loading" class="text-center col-xs-12" ></span>
    </div>
</div>
<div class="modal-actions ">
    <button type="submit " class="btn-link modal-action ">
        <strong><span class="icon icon-user "></span>postImg</strong>
    </button>
    <button type="button" id="fileinput"   ng-model="files" ngf-select accept="image/* " data-ngf-multiple="true" class="btn-link modal-action">Upload Image

现在我想通过json为我的地图设置一个特定的MapStyle,我看到了这段代码:

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
    }
    }

但是我不知道如何整合,因为这个示例是用于活动而不是我使用片段,当我尝试添加此代码时给我一个错误。 有帮助吗? 感谢

1 个答案:

答案 0 :(得分:0)

经过深入研究和编码后,我使用以下代码在片段中实现了Google Map:

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);
    }
}

以下是我用于地图片段

的xml代码
<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>

在这个片段中,您可以通过排球获取JSON并相应地设置地图样式。