上一讲中我们学会了如何在MapView中添加Graphic要素,那么在百度或高德地图中,当我们点击要素时,会显示出相应的详细信息。在GraphicsLayer中也提供了这样的方法。下面我们来学习在GraphicsLayer中如何点击查找要素。 首先在MapView中添加两个Graphic要素。代码如下,注意这里用Graphic(Geometry geometry, Symbol symbol, Map<String, Object> attributes)来实例化Graphic,Map<String, Object> attributes是要素的属性值。详细见以下代码。TILED_WORLD_STREETS_URL为网络图层地址。

  1. private void initLayer() {
  2. mapView.addLayer(new ArcGISTiledMapServiceLayer(
  3. TILED_WORLD_STREETS_URL));
  4. graphicsLayer = new GraphicsLayer();
  5. mapView.addLayer(graphicsLayer);
  6. Polygon polygon = new Polygon();
  7. polygon.startPath(new Point(1.2575908509778766E7,2879410.9266042486));
  8. polygon.lineTo(new Point(1.284360696117901E7,3021972.232083669));
  9. polygon.lineTo(new Point(1.2826182801620414E7,2713089.403544925));
  10. Map<String, Object> attr1 = new HashMap<>();
  11. attr1.put("name", "广州");
  12. attr1.put("mark", "广州是南方的城市");
  13. Graphic graphic1 = new Graphic(polygon, new SimpleFillSymbol(Color.RED),attr1);
  14. graphicsLayer.addGraphic(graphic1);
  15. Polygon polygon2 = new Polygon();
  16. polygon2.startPath(new Point(1.3388507951011453E7,3611225.628065273));
  17. polygon2.lineTo(new Point(1.3607101952746565E7,3858331.890896268));
  18. polygon2.lineTo(new Point(1.3613438010767872E7,3449656.14852193));
  19. Map<String, Object> attr2 = new HashMap<>();
  20. attr2.put("name", "上海");
  21. attr2.put("mark", "上海是中部的城市");
  22. Graphic graphic2 = new Graphic(polygon2, new SimpleFillSymbol(Color.GREEN),attr2);
  23. graphicsLayer.addGraphic(graphic2);
  24. }

效果图如下:

准备工作完成后,设置MapView的点击事件,

  1. mapView.setOnSingleTapListener(new OnSingleTapListener() {
  2. @Override
  3. public void onSingleTap(float x, float y) {
  4. // TODO Auto-generated method stub
  5. handleSingleTap(x,y);
  6. }
  7. });

在handleSingleTap方法中来处理查询事件,GraphicsLayer查询要用到getGraphicIDs(float x, float y, int tolerance, int numberOfResults)或者getGraphicIDs(float x, float y, int tolerance)方法,前面两个参数是地图点击时的

x与y的值,tolerance是围绕x与y这个点所查询的范围,numberOfResults是要返回结果的大小。

  1. /**
  2. * GraphicsLayer的点击查询
  3. * @param x
  4. * @param y
  5. */
  6. protected void handleSingleTap(float x, float y) {
  7. int[] graphicIds = graphicsLayer.getGraphicIDs(x, y, 8);
  8. if (graphicIds!=null&&graphicIds.length>0) {
  9. for (int i = 0; i < graphicIds.length; i++) {
  10. Graphic graphic = graphicsLayer.getGraphic(graphicIds[i]);
  11. Map<String,Object> attr = graphic.getAttributes();
  12. Log.i(TAG, attr.get("name")+"===="+attr.get("mark"));
  13. }
  14. }
  15. }这样当我们点击要素时,会打出以下的信息。

Android GIS开发系列-- 入门季(4) GraphicsLayer的点击查询要素的更多相关文章

  1. Android GIS开发系列-- 入门季(14)FeatureLayer之范围查询

    Android GIS开发系列-- 入门季(5),这篇文章中,我们知道如何去查找要素.现在有一个需求,查找某点5000米范围的要素,那如何来做呢?首先我们需要在地图上画个5000米半径的圆,然后根据Q ...

  2. Android GIS开发系列-- 入门季(5) FeatureLayer加载本地shp文件与要素查询

    FeatureLayer是要素图层,也是Arcgis的主要图层.用这个图层可以加载本地的shp文件.下面我们看怎样加载shp文件到MapView中.查看ArcGis API可知FeatureLayer ...

  3. Android GIS开发系列-- 入门季(3) GraphicsLayer添加点、线、面

    GraphicsLayer是图形图层,可以自定义图形添加到地图上.调用GraphicsLayer的addGraphic方法就能添加图形,此方法要一个Graphic对象,此对象的构造方法是Graphic ...

  4. Android GIS开发系列-- 入门季(6)GraphicsLayer添加文字与图片标签

    一.GraphicsLayer添加图片 GraphicLayer添加图片Graphic,要用到PictureMarkerSymbol,也是样式的一种.添加代码如下: Drawable drawable ...

  5. Android GIS开发系列-- 入门季(1) 起点

    前言 这个系列,待最终完成更新,大家体谅点,第一版本全部是参考的网络教程,最近会逐步的细化更新为可以直接使用的情况. 本系列的开发基于AS (  Android Studio ), 和ArcGIS 的 ...

  6. Android GIS开发系列-- 入门季(2) MapView与图层介绍

    一.MapView MapView是Arcgis中的最基本的类,与高德地图SDK的MapView的重要性一样.MapView的创建有两种方法,一种是在Layout文件中直接写控件.一种是实例化,Map ...

  7. Android GIS开发系列-- 入门季(13)Gdal简单写个shp文件

    Gdal是用来读写栅格与矢量数据的,在Gdal官网,可以下载相关的资源进行平台的编译.其实Arcgis底层也是用Gdal来读取shp文件的,那在Android中可以直接读写shp文件吗,是可以的.这里 ...

  8. Android GIS开发系列-- 入门季(12) 显示载天地图

    在项目中可以经常需要动态加载一些图层,像投影地图服务.投影地图服务器.其实网上有大量这样的服务,比如天地图官网, . 随便点开一个服务,里面有相关的信息.那如何加载这样图层服务呢. 一.首先感谢这篇博 ...

  9. Android GIS开发系列-- 入门季(10) MapView快速定位到Geometry

    我们知道某个Geometry的坐标,但不知道具体的位置,该如何使地图快速定位呢?这时需要用到MapView.setExtent方法,来看下这个方法的介绍:Zooms the map to the gi ...

随机推荐

  1. Android 性能优化(13)网络优化( 9)Determining and Monitoring the Docking State and Type

    Determining and Monitoring the Docking State and Type PreviousNext This lesson teaches you to Determ ...

  2. 398 Random Pick Index 随机数索引

    给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引. 您可以假设给定的数字一定存在于数组中.注意:数组大小可能非常大. 使用太多额外空间的解决方案将不会通过测试.示例:int[] num ...

  3. vscode使用教程(web开发)

    1.安装 进入官网下载https://code.visualstudio.com/ 一直下一步就好了,中间可以选择把软件安装在哪个目录. 2.常用插件安装 a. 进入扩展视图界面安装/卸载 a1.快捷 ...

  4. python manage.py syncdb报错:No module named MySQLdb

    今天同步数据时出现这个错误: 解决方法: 1.先下载mysql-python 支持1.2.3-2.7版本 MySQL-python 1.2.3 for Windows and Python 2.7, ...

  5. Tornado引入静态css、js文件

    一.静态路径 template_path=os.path.join(os.path.dirname(__file__), "templates") 这里是设置了模板的路径,放置模板 ...

  6. Java:一个简捷的可分页的ResultSet实现

    内容 前言 JDBC和分页 和具体数据库相关的实现方法 另一种繁琐的实现方法 使用Vector进行分页 一个新的Pageable接口及其实现 Pageable的使用方法 总结 参考资料 关于作者 前言 ...

  7. 关于c++11中static类对象构造函数线程安全的验证

    在c++11中,static静态类对象在执行构造函数进行初始化的过程是线程安全的,有了这个特征,我们可以自己动手轻松的实现单例类,关于如何实现线程安全的单例类,请查看c++:自己动手实现线程安全的c+ ...

  8. Codeforces_766_C_(dp)

    C. Mahmoud and a Message time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  9. Solr搜索引擎 — 通过mysql配置数据源

    一,准备数据库数据表结构 CREATE TABLE `app` ( `id` int(11) NOT NULL AUTO_INCREMENT, `app_name` varchar(255) NOT ...

  10. day17-常用模块II (hashlib、logging)

    目录 hashlib模块 撞库破解hash算法加密 logging模块 配置日志文件 hashlib模块 一般用于明文加密,其实就是一个自定义的字符编码表.原来0和1转换成字符,而现在的是字符转成另一 ...