arcgis for Android 100.1 基本操作(缩小放大旋转定位)

时间:2021-01-24 23:44:03

arcgis for Android 100多是17年发布的。跟之前的10点多系列变化还是很大,很多地方都是重构过的。功能的强大就不用我多说。这篇先讲地图的基本操作以及加载地图。后面会说怎么加载天地图,谷歌地图等,加载shp,画图等等。

github上面官方示例程序:https://github.com/Esri/arcgis-runtime-samples-android

1,上图

arcgis for Android 100.1 基本操作(缩小放大旋转定位)

arcgis for Android 100.1 基本操作(缩小放大旋转定位)

2,代码实现

package com.arcgis.activity;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;

import com.arcgis.R;
import com.esri.arcgisruntime.layers.ArcGISTiledLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.view.MapView;

public class ArcgisMapActivity extends AppCompatActivity implements View.OnClickListener {
    private String mapServerUrl = "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer";
    private MapView mMapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_1);
        mMapView = (MapView) findViewById(R.id.map_view);

        initView();
    }

    private void initView() {
        findViewById(R.id.button4).setOnClickListener(this);
        findViewById(R.id.button5).setOnClickListener(this);
        findViewById(R.id.button6).setOnClickListener(this);

        ArcGISTiledLayer tiledLayer = new ArcGISTiledLayer(mapServerUrl);

        Basemap basemap = new Basemap(tiledLayer);

        ArcGISMap map = new ArcGISMap(basemap);

        mMapView.setMap(map);

    }


    protected void onResume() {
        super.onResume();
        mMapView.resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.pause();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button4://放大
                double scale = mMapView.getMapScale();
                mMapView.setViewpointScaleAsync(scale * 2);
                break;
            case R.id.button5://缩小
                double scale1 = mMapView.getMapScale();
                mMapView.setViewpointScaleAsync(scale1 * 0.5);
                break;
            case R.id.button6://旋转
                mMapView.setViewpointRotationAsync(90);
                break;

        }

    }

}

最基本的地图加载,以及地图放大,缩小,以及旋转。

package com.arcgis.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.widget.Toast;

import com.arcgis.R;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.DefaultMapViewOnTouchListener;
import com.esri.arcgisruntime.mapping.view.MapView;

import java.text.SimpleDateFormat;
import java.util.Locale;


public class ArcgisMapTestActivity extends AppCompatActivity {
    private MapView mMapView;

    private SimpleDateFormat mSimpleDateFormatter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        mMapView = (MapView) findViewById(R.id.map_view);

        // create a simple date formatter to parse strings to date
        mSimpleDateFormatter = new SimpleDateFormat(getString(R.string.date_format), Locale.US);

        // inflate MapView from layout
        mMapView = (MapView) findViewById(R.id.map_view);

        // create a map with the BasemapType topographic
        //内置的地图数据源
        ArcGISMap map = new ArcGISMap(Basemap.createImageryWithLabelsVector());

        //center for initial viewpoint
        // 这个Point是按照经纬度模式 跟osmdroid定位刚好反过来
        //patialReference.create(4326) 参考坐标系   4326 是WGS84 的坐标
        Point center = new Point(113.365548756, 23.12648183, SpatialReference.create(4326));

        //set initial viewpoint
        //9000 是缩放比例   这缩放比例参数也是独树一帜啊
        map.setInitialViewpoint(new Viewpoint(center, 9000));

        // set the map to the map view
        mMapView.setMap(map);


        mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, mMapView) {
            @Override
            public boolean onSingleTapConfirmed(MotionEvent v) {
                android.graphics.Point screenPoint = new android.graphics.Point(Math.round(v.getX()), Math.round(v.getY()));
                Point clickPoint = mMapView.screenToLocation(screenPoint);
                mMapView.setViewpointCenterAsync(clickPoint).addDoneListener(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(ArcgisMapTestActivity.this, "平移到这里", Toast.LENGTH_SHORT).show();
                    }
                });
                return true;
            }

            @Override
            public boolean onDoubleTapEvent(MotionEvent e) {
                Toast.makeText(ArcgisMapTestActivity.this, "双击了一下", Toast.LENGTH_SHORT).show();
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                Toast.makeText(ArcgisMapTestActivity.this, "长按了地图一下", Toast.LENGTH_SHORT).show();
            }
        });
    }


    protected void onResume() {
        super.onResume();
        mMapView.resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.pause();
    }
}

定位坐标的换算。一般来说地图都是WGS84的世界坐标系,进行绘图的。但是你本地的地方有自已的地方坐标的。所以你定位时候,哟啊声明这个坐标是在什么坐标系的坐标。

然后进行定位。

地图的一些操作,平移,双击,长按事件等

3,权限

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

上网以及存储,定位的gps权限。