GoogleMapActivity.java 11.6 KB
package com.pashanhoo.landsurvey;

import android.location.Location;
import android.location.LocationListener;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.JSONObjectRequestListener;
import com.arlib.floatingsearchview.FloatingSearchView;
import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion;
import com.esri.android.map.Callout;
import com.esri.android.map.CalloutStyle;
import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.LocationDisplayManager;
import com.esri.android.map.MapView;
import com.esri.android.runtime.ArcGISRuntime;
import com.esri.core.geometry.Point;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.PictureMarkerSymbol;
import com.pashanhoo.landsurvey.layers.gaode.GaodeMapLayerTypes;
import com.pashanhoo.landsurvey.layers.gaode.GaodeMapsTiledServiceLayer;
import com.pashanhoo.landsurvey.layers.google.GoogleMapsTiledServiceLayer;
import com.pashanhoo.landsurvey.layers.tencent.TencentMapLayerTypes;
import com.pashanhoo.landsurvey.layers.tencent.TencentMapsTiledServiceLayer;
import com.pashanhoo.landsurvey.search.POISuggestion;
import com.pashanhoo.landsurvey.utils.AppInfo;
import com.pashanhoo.landsurvey.utils.GISHelper;
import com.pashanhoo.landsurvey.utils.JZLocationConverter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class GoogleMapActivity extends AppCompatActivity {

    private MapView mapView;
    // 地图中的GraphicsLayer对象
    private GraphicsLayer graphicsLayer;
    // GPS定位器对象
    private LocationDisplayManager locationDisplayManager;
    // 浮动查询条
    private FloatingSearchView floatingSearchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_google_map);
        floatingSearchView = (FloatingSearchView) findViewById(R.id.floating_search_view);

        AppInfo.Settings();

        ArcGISRuntime.setClientId("1eFHW78avlnRUPHm");
        mapView = (MapView) findViewById(R.id.map);
        mapView.setEsriLogoVisible(false);
        mapView.enableWrapAround(false);

//        GoogleMapsTiledServiceLayer GOOGLE_VECTOR_Layer = new GoogleMapsTiledServiceLayer(GoogleMapLayerTypes.VECTOR_GOOGLE_MAP);
//        GoogleMapsTiledServiceLayer GOOGLE_TER_Layer = new GoogleMapsTiledServiceLayer(GoogleMapLayerTypes.TERRAIN_GOOGLE_MAP);
//        GoogleMapsTiledServiceLayer GOOGLE_IMAGE_Layer = new GoogleMapsTiledServiceLayer(GoogleMapLayerTypes.IMAGE_GOOGLE_MAP);
//        mapView.addLayer(GOOGLE_IMAGE_Layer);
//        GoogleMapsTiledServiceLayer GOOGLE_ANNO_Layer = new GoogleMapsTiledServiceLayer(GoogleMapLayerTypes.IMAGE_ANNO_GOOGLE_MAP);
//        mapView.addLayer(GOOGLE_ANNO_Layer);
//        GoogleMapsTiledServiceLayer OSM_Layer = new GoogleMapsTiledServiceLayer(GoogleMapLayerTypes.OSM_Map);
//        mapView.addLayer(GOOGLE_VECTOR_Layer);

        //---------------------------------------------------------------------------------------------------------------------
        // 高德矢量图层(含路网,含注记)
        final GaodeMapsTiledServiceLayer gaodeMapsTiledServiceLayer = new GaodeMapsTiledServiceLayer(GaodeMapLayerTypes.AMAP_VECTOR);
        // 高德影像图层(不含路网,不含注记)
//        GaodeMapsTiledServiceLayer gaodeMapsTiledServiceLayer = new GaodeMapsTiledServiceLayer(GaodeMapLayerTypes.AMAP_IMAGE);
        // 高德路网图层(含路网,含注记)
//        GaodeMapsTiledServiceLayer gaodeMapsTiledServiceLayer = new GaodeMapsTiledServiceLayer(GaodeMapLayerTypes.AMAP_ROAD);
        // 高德实时交通图层
//        GaodeMapsTiledServiceLayer gaodeMapsTiledServiceLayer = new GaodeMapsTiledServiceLayer(GaodeMapLayerTypes.AMAP_TRAFFIC);
        mapView.addLayer(gaodeMapsTiledServiceLayer);
        //---------------------------------------------------------------------------------------------------------------------

        TencentMapsTiledServiceLayer tencentMapsTiledServiceLayer = new TencentMapsTiledServiceLayer(TencentMapLayerTypes.TENCENT_MAP_VECTOR);
        mapView.addLayer(tencentMapsTiledServiceLayer);

        //---------------------------------------------------------------------------------------------------------------------
//        GeoQMapsTiledServiceLayer geoQMapsTiledServiceLayer = new GeoQMapsTiledServiceLayer(GeoQMapLayerTypes.ChinaOnlineCommunity_Mobile);
//        mapView.addLayer(geoQMapsTiledServiceLayer);

        // 添加GraphicsLayer
        graphicsLayer = new GraphicsLayer();
        mapView.addLayer(graphicsLayer);

        mapView.setMaxResolution(156543.03392800014);
        mapView.setMinResolution(0.29858214164761665);


        Point curPt = JZLocationConverter.wgs84ToGcj02(AppInfo.initPoint);
        Point pt = GISHelper.lonLat2Mercator(curPt);
        mapView.zoomToResolution(pt, GoogleMapsTiledServiceLayer.getRes()[17]);

        locationDisplayManager = mapView.getLocationDisplayManager();
        locationDisplayManager.setShowLocation(true);
        locationDisplayManager.setAutoPanMode(LocationDisplayManager.AutoPanMode.LOCATION);//设置模式
        locationDisplayManager.setShowPings(true);
        locationDisplayManager.setLocationListener(new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Point pt = new Point(location.getLongitude(), location.getLatitude());
                Point gcjPt = JZLocationConverter.wgs84ToGcj02(pt);
                mapView.zoomToResolution(GISHelper.lonLat2Mercator(gcjPt),
                        GoogleMapsTiledServiceLayer.getRes()[18]);
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {
            }

            @Override
            public void onProviderEnabled(String s) {
            }

            @Override
            public void onProviderDisabled(String s) {
            }
        });

        FloatingActionButton gps = (FloatingActionButton) findViewById(R.id.gps);
        gps.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 设置GPS功能
                if (locationDisplayManager.isStarted()) {
                    locationDisplayManager.stop();
                } else {
                    locationDisplayManager.start();
                    mapView.zoomToResolution(GISHelper.lonLat2Mercator(locationDisplayManager.getPoint()),
                            GoogleMapsTiledServiceLayer.getRes()[18]);

                    Point cupt = GISHelper.mercator2lonLat(mapView.getCenter());
                    Toast.makeText(GoogleMapActivity.this,
                            cupt.getX() + ":" + cupt.getY(),
                            Toast.LENGTH_LONG).show();
                }
            }
        });

        // 处理浮动查询条
        floatingSearchView.setOnSearchListener(new FloatingSearchView.OnSearchListener() {
            @Override
            public void onSuggestionClicked(SearchSuggestion searchSuggestion) {
                graphicsLayer.removeAll();

                POISuggestion colorSuggestion = (POISuggestion) searchSuggestion;
                Point poipt = new Point(colorSuggestion.getLng(), colorSuggestion.getLat());
                mapView.zoomToResolution(GISHelper.lonLat2Mercator(poipt), gaodeMapsTiledServiceLayer.getRes()[19]);

                graphicsLayer.addGraphic(new Graphic(GISHelper.lonLat2Mercator(poipt),
                        new PictureMarkerSymbol(getResources().getDrawable(R.mipmap.locator)).setOffsetY(16)));

                LayoutInflater inflater = LayoutInflater.from(GoogleMapActivity.this);
                View view = inflater.inflate(R.layout.callout_layout, null);
                // 设置Callout属性
                TextView title = (TextView)view.findViewById(R.id.title);
                TextView content = (TextView)view.findViewById(R.id.content);
                title.setText("名称:" + colorSuggestion.getName());
                content.setText("地址:" + colorSuggestion.getAddress());
                // 获取Callout
                Callout callout = mapView.getCallout();
                callout.setContent(view);
                // 设置Callout样式
                callout.setStyle(R.xml.calloutlayout);
                // 设置锚点偏移量
                callout.setOffset(0, -5);
                // 显示Callout
                callout.refresh();
                callout.show(GISHelper.lonLat2Mercator(poipt));
                callout.setMaxHeight(400);
                callout.setMaxWidth(600);
            }

            @Override
            public void onSearchAction(String currentQuery) {
                if (currentQuery.length() >= 3) {
                    floatingSearchView.clearSuggestions();
                    // 查询天地图API
                    String str = "http://restapi.amap.com/v3/place/text?keywords="+ currentQuery
                            +"&output=json&offset=15&page=1&key=f1713c28f00da34d6ee23dccb7f74b81&extensions=all";
                    AndroidNetworking.get(str)
                            .build()
                            .getAsJSONObject(new JSONObjectRequestListener() {
                                @Override
                                public void onResponse(JSONObject response) {
                                    //解析响应结果
                                    JSONArray Jarray = null;
                                    try {
                                        Jarray = response.getJSONArray("pois");
                                        ArrayList<POISuggestion> poiSuggestions = new ArrayList<POISuggestion>();
                                        for (int i = 0; i < Jarray.length(); i++) {
                                            JSONObject Jasonobject = Jarray.getJSONObject(i);
                                            poiSuggestions.add(new POISuggestion(Jasonobject.getString("name"),
                                                    Jasonobject.getString("address"),
                                                    Jasonobject.getString("location").replace(",", " ")));
                                        }
                                        floatingSearchView.swapSuggestions(poiSuggestions);
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }

                                @Override
                                public void onError(ANError anError) {
                                    Log.i("error", anError.getMessage());
                                }
                            });
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.unpause();
        if (locationDisplayManager != null) {
            locationDisplayManager.resume();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.pause();
        if (locationDisplayManager != null) {
            locationDisplayManager.pause();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (locationDisplayManager != null) {
            locationDisplayManager.stop();
        }

    }
}