您的位置:首页 > 移动开发 > Android开发

arcgis for android 学习 - (2) hello Wrold. 显示一个地图

2015-08-14 10:23 417 查看
arcgis for android API包 里有一些例子,建议先打开hello World示例看看。我就是看了点例子,然后从网上的高人们的blog里找资料学习的。
先让我们来做个示例。如果不让我看到点成就的页面,我怕觉得自己没掌握到任何东西。

下面是我个人的一些理解:

1. MapView 是android中的以个viewGroup对象,它继承具有viewGroup的属性。它是地图视图的主面板。或者说是一个地图视图的入口。

2. 我们可以往MapView 下(内)添加多个图层(layer)。

MapView map;

map.addLayer(...)

3.我们可以构建一些地图服务图层对象。该服务需要一个 URL的服务地址。

new com.esri.android.map.ags.ArcGISTiledMapServiceLayer(

"http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaCities_Community_BaseMap_ENG/BeiJing_Community_BaseMap_ENG/MapServer")

这里构建以个ArcGISTiledMapServiceLayer对象。

4.添加图层。 并将该“服务图层”附件到mapView以下。

ArcGISTiledMapServiceLayer agmServiceLayer;

map.addLayer(agmServiceLayer)

4. mapView可以指定一个坐标位置。该坐标位置确定了“要显示地图的范围区域”。

Envelope initextext = new Envelope(12899459.4956466,4815363.65520802,13004178.2243971,4882704.67712717);//这里有4个坐标点,看似是一个矩形的4个顶点。

map.setExtent(initextext);//设定。。。
好吧,让我们跑起来代码。

-----------

贴完整代码如下:

布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<Button
android:id="@+id/btnToBeijing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="到北京" />

<Button
android:id="@+id/btnToShenyang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="到沈阳" />
</LinearLayout>

<!-- MapView layout and initial extent -->

<com.esri.android.map.MapView
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</com.esri.android.map.MapView>

</LinearLayout>

代码:

/* Copyright 2012 ESRI

*
* All rights reserved under the copyright laws of the United States
* and applicable international laws, treaties, and conventions.
*
* You may freely redistribute and use this sample code, with or
* without modification, provided you include the original copyright
* notice and use restrictions.
*
* See the �Sample code usage restrictions� document for further information.
*
*/

package com.esri.arcgis.android.samples.helloworld;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISDynamicMapServiceLayer;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Geometry;

public class HelloWorld extends Activity {
MapView map = null;
ArcGISTiledMapServiceLayer tileLayer;
Button m_btnToShenyang;
Button m_btnToBeijing;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

m_btnToShenyang = (Button)findViewById(R.id.btnToShenyang);
m_btnToShenyang.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// Add dynamic layer to MapView
map.addLayer(new com.esri.android.map.ags.ArcGISTiledMapServiceLayer("" +
"http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaCities_Community_BaseMap_ENG/ShenYang_Community_BaseMap_ENG/MapServer"));

Envelope initextext = new Envelope(13700260.2294766, 5108777.85728174, 13769970.7992726, 5150359.60066882);
map.setExtent(initextext);
}
});

m_btnToBeijing = (Button)findViewById(R.id.btnToBeijing);
m_btnToBeijing.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// Add dynamic layer to MapView
map.addLayer(new com.esri.android.map.ags.ArcGISTiledMapServiceLayer("" +
"http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaCities_Community_BaseMap_ENG/BeiJing_Community_BaseMap_ENG/MapServer"));

Envelope initextext = new Envelope(12899459.4956466,4815363.65520802,13004178.2243971,4882704.67712717);
map.setExtent(initextext);
}
});

// Retrieve the map and initial extent from XML layout
map = (MapView)findViewById(R.id.map);
// Add tiled layer to MapView
tileLayer = new ArcGISTiledMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
map.addLayer(tileLayer);
Envelope initextext = new Envelope(-20037507.0671618,-20037507.0671618, 20037507.0671618, 20037507.0671619);
map.setExtent(initextext);
}

protected void onPause() {
super.onPause();
map.pause();
}

protected void onResume() {
super.onResume();
map.unpause();
}

}

参考:

sydbc 的博客: http://www.gisall.com/html/72/124272-6268.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: