热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

arcgisforandroid添加标记,并选中它

1.我们的需求是在地图上面添加一个点进行标记,并且点击的时候可以选中它布局文件:<?xmlversion1.0encodingutf-8?><Line

1.我们的需求是在地图上面添加一个点进行标记,并且点击的时候可以选中它

这里写图片描述

布局文件:


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

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


<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加一个要素" />

LinearLayout>


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

代码:

 package demo.GraphicsLayerLib;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.InfoTemplate;
import com.esri.android.map.Layer;
import com.esri.android.map.MapView;
import com.esri.android.map.event.OnSingleTapListener;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.Point;
import com.esri.core.map.Graphic;
import com.esri.core.renderer.SimpleRenderer;
import com.esri.core.symbol.PictureMarkerSymbol;
import com.esri.core.symbol.Symbol;

public class GraphicsLayerDemoActivity extends Activity {
Button btn1;
MapView mMapView;
final String URL_STREET_COLD = "http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaOnlineStreetCold/MapServer";
GraphicsLayer mGraphicsLayer;
final int STATE_ADD_GRAPHIC = 1; // 进入 “添加graphics状态,这时候单击地图时操作就添加graphics
final int STATE_SHOW = 2;// “选中graphics状态“,这时候单击地图时操作就
// 选择一个graphics,并显示该graphics的附加信息”
int m_State;// 状态

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

m_State = STATE_SHOW;

btn1 = (Button) findViewById(R.id.btn1);
btn1.setText("准备添加要素");
btn1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// 切换按钮状态,第一次点击本按钮后进入 “添加graphics状态,这时候单击地图时操作就添加graphics”
// 第一次点击本按钮后进入 “选中graphics状态“,这时候单击地图时操作就
// 选择一个graphics,并显示该graphics的附加信息”
m_State = m_State == STATE_ADD_GRAPHIC ? STATE_SHOW
: STATE_ADD_GRAPHIC;
if (m_State == STATE_ADD_GRAPHIC) {
btn1.setText("单击地图将添加要素,单击本按钮结束");
} else {
btn1.setText("准备添加要素");
}
}
});

mMapView = (MapView) findViewById(R.id.map);
// Add layer to MapView
mMapView.addLayer(new com.esri.android.map.ags.ArcGISTiledMapServiceLayer(
URL_STREET_COLD));

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

// 设定单击事件
mMapView.setOnSingleTapListener(m_OnSingleTapListener);
}

OnSingleTapListener m_OnSingleTapListener= new OnSingleTapListener() {
int m_Char = 65;

public void onSingleTap(float x, float y) {
if (!mMapView.isLoaded()) {
return;
}
if (m_State == STATE_ADD_GRAPHIC) {
// 获得图层
AddNewGraphic(x, y);
} else {// 选中 Graphics
SelectOneGraphic(x, y);
}// end if else
}// end method


private void SelectOneGraphic(float x, float y) {
// 获得图层
GraphicsLayer layer = GetGraphicLayer();
if (layer != null && layer.isInitialized() && layer.isVisible()) {
Graphic result = null;
// 检索当前 光标点(手指按压位置)的附近的 graphic对象
result = GetGraphicsFromLayer(x, y, layer);
if (result != null) {
// 获得附加特别的属性
String msgTag = (String) result
.getAttributeValue("tag");
// 显示提示
AlertMsg(msgTag);
}// end if
}// end if
}

private void AddNewGraphic(float x, float y) {
GraphicsLayer layer = GetGraphicLayer();
if (layer != null && layer.isInitialized() && layer.isVisible()) {
// 转换坐标
Point pt = mMapView.toMapPoint(new Point(x, y));
// 附加特别的属性
Map map = new HashMap();
map.put("tag", "" + (char) (m_Char++));
// 创建 graphic对象
Graphic gp1 = CreateGraphic(pt, map);
// 添加 Graphics 到图层
layer.addGraphic(gp1);
}
}
};

/**
* 从一个图层里里 查找获得 Graphics对象. x,y是屏幕坐标,layer
* 是GraphicsLayer目标图层(要查找的)。相差的距离是50像素。
* @param xScreen
* @param yScreen
* @param layer
* @return
*/

private Graphic GetGraphicsFromLayer(double xScreen, double yScreen) {
Graphic result = null;
try {
int[] idsArr = layer.getGraphicIDs();
Log.d("TestNet","一共:"+idsArr.length);
double x = xScreen;
double y = yScreen;
for (int i = 0; i Graphic gpVar = layer.getGraphic(idsArr[i]);
if (gpVar != null) {
Log.d("TestNet","<<>>"+gpVar.getGeometry());
if(gpVar.getGeometry().toString().contains("Point")){

Point pointVar = (Point) gpVar.getGeometry();
pointVar = mArcgisMapView.toScreenPoint(pointVar);
double x1 = pointVar.getX();
double y1 = pointVar.getY();
if (Math.sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1)) <50) {
result = gpVar;
break;
}else{
}

}
}
}
} catch (Exception e) {
return null;
}
return result;
}

/*
* 创建一个Graphic , 参数geometry是屏幕坐标位置,map是附加的属性参数
*/

private Graphic CreateGraphic(Point geometry, Map map) {
GraphicsLayer layer = GetGraphicLayer();// 获得图层
Drawable image = GraphicsLayerDemoActivity.this.getBaseContext()
.getResources().getDrawable(R.drawable.pop);
PictureMarkerSymbol symbol = new PictureMarkerSymbol(image);

// 构建graphic
// Graphic g = new Graphic(geometry, symbol);
Graphic g = new Graphic(geometry, symbol, map, null);
return g;
}

/*
* 获得 GetGraphicLayer
*/

private GraphicsLayer GetGraphicLayer() {
if (mGraphicsLayer == null) {
mGraphicsLayer = new GraphicsLayer();
mMapView.addLayer(mGraphicsLayer);
}
return mGraphicsLayer;
}

void AlertMsg(String str, Object... arg) {
String msg = String.format(str, arg);
Toast.makeText(this, msg, 2).show();
Log.i("AlertMsg", msg);
}

@Override
protected void onDestroy() {
super.onDestroy();
}

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

@Override
protected void onResume() {
super.onResume();
mMapView.unpause();
}

}

资源链接地址

http://download.csdn.net/detail/xzytl60937234/9728384


推荐阅读
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文介绍了使用kotlin实现动画效果的方法,包括上下移动、放大缩小、旋转等功能。通过代码示例演示了如何使用ObjectAnimator和AnimatorSet来实现动画效果,并提供了实现抖动效果的代码。同时还介绍了如何使用translationY和translationX来实现上下和左右移动的效果。最后还提供了一个anim_small.xml文件的代码示例,可以用来实现放大缩小的效果。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • 今天就跟大家聊聊有关怎么在Android应用中实现一个换肤功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
  • 本文介绍了pack布局管理器在Perl/Tk中的使用方法及注意事项。通过调用pack()方法,可以控制部件在显示窗口中的位置和大小。同时,本文还提到了在使用pack布局管理器时,应注意将部件分组以便在水平和垂直方向上进行堆放。此外,还介绍了使用Frame部件或Toplevel部件来组织部件在窗口内的方法。最后,本文强调了在使用pack布局管理器时,应避免在中间切换到grid布局管理器,以免造成混乱。 ... [详细]
  • 本文讨论了在使用Git进行版本控制时,如何提供类似CVS中自动增加版本号的功能。作者介绍了Git中的其他版本表示方式,如git describe命令,并提供了使用这些表示方式来确定文件更新情况的示例。此外,文章还介绍了启用$Id:$功能的方法,并讨论了一些开发者在使用Git时的需求和使用场景。 ... [详细]
  • 查找给定字符串的所有不同回文子字符串原文:https://www ... [详细]
author-avatar
dgh
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有