Android GIS开发系列-- 入门季(3) GraphicsLayer添加点、线、面
GraphicsLayer是图形图层,可以自定义图形添加到地图上。调用GraphicsLayer的addGraphic方法就能添加图形,此方法要一个Graphic对象,此对象的构造方法是Graphic(Geometry geometry, Symbol symbol),前面第一个是要素,而Geometry的子类有Envelope, MultiPath, MultiPoint, Point, Segment,Line, Polygon, Polyline。我们所常用的有点Point,线Polyline,面Polygon。而后面一
个参数是样式,点的样式是SimpleMarkerSymbol,线的样式是SimpleLineSymbol,面的样式是SimpleFillSymbol。
一、GraphicsLayer添加点:
- Graphic graphic = new Graphic(point, new SimpleMarkerSymbol(Color.RED,5, SimpleMarkerSymbol.STYLE.CIRCLE));
- graphicsLayer.addGraphic(graphic);
二、添加线,样式有实线、虚线、点虚线、点点虚线、点线等。效果图如下:
- Polyline polyline = new Polyline();
- polyline.startPath(new Point(113,23));//第一个点用startPath,后面的点用lineTo方法
- polyline.lineTo(new Point(123,23));
- graphic = new Graphic(polyline,new SimpleLineSymbol(Color.RED,3, SimpleLineSymbol.STYLE.SOLID));
- graphicsLayer.addGraphic(graphic);
三、添加面Polygon,效果图如下:
- Polygon polygon = new Polygon();
- //第一个点startPath,后面的点用lineTo
- polygon.startPath(new Point(118,23));
- polygon.lineTo(new Point(118,15));
- polygon.lineTo(new Point(113,13));
- graphic = new Graphic(polygon,new SimpleFillSymbol(Color.GREEN,SimpleFillSymbol.STYLE.SOLID));
- graphicsLayer.addGraphic(graphic);
Code for all:
- package com.arcgis.test;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.Button;
- import android.widget.Spinner;
- import com.esri.android.map.GraphicsLayer;
- import com.esri.android.map.MapView;
- import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;
- import com.esri.android.map.event.OnSingleTapListener;
- import com.esri.core.geometry.Point;
- import com.esri.core.geometry.Polygon;
- import com.esri.core.geometry.Polyline;
- import com.esri.core.map.Graphic;
- import com.esri.core.symbol.SimpleFillSymbol;
- import com.esri.core.symbol.SimpleLineSymbol;
- import com.esri.core.symbol.SimpleMarkerSymbol;
- import java.util.ArrayList;
- import java.util.List;
- public class GraphicActivity extends AppCompatActivity {
- private MapView mMapView;
- private Spinner graphicTypeSpinner;
- private Button clernBtn;
- private ArcGISTiledMapServiceLayer arcGISTiledMapServiceLayer;
- private GraphicsLayer graphicsLayer;
- private String mapServerUrl = "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer";
- //点集合
- private List<Point> pointList = new ArrayList<>();
- private Graphic graphic;
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_graphic);
- initView();
- addLayer();
- }
- private void initView() {
- mMapView = (MapView) findViewById(R.id.map_view);
- //mapview点击事件
- mMapView.setOnSingleTapListener(new OnSingleTapListener() {
- @Override
- public void onSingleTap(float x, float y) {
- handleSingleTap(x, y);
- }
- });
- graphicTypeSpinner = (Spinner) findViewById(R.id.spinner_type);
- graphicTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
- pointList.removeAll(pointList);
- graphicsLayer.removeAll();
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- }
- });
- clernBtn = (Button) findViewById(R.id.clear_graphic);
- clernBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- pointList.removeAll(pointList);
- graphicsLayer.removeAll();
- }
- });
- }
- private void addLayer() {
- arcGISTiledMapServiceLayer = new ArcGISTiledMapServiceLayer(mapServerUrl);
- mMapView.addLayer(arcGISTiledMapServiceLayer);
- graphicsLayer = new GraphicsLayer();
- mMapView.addLayer(graphicsLayer);
- }
- private void handleSingleTap(float x, float y) {
- Point point = mMapView.toMapPoint(x, y);
- graphicsLayer.removeAll();
- pointList.add(point);
- String type = graphicTypeSpinner.getSelectedItem().toString().trim();
- switch (type) {
- case "点":
- graphic = new Graphic(point, new SimpleMarkerSymbol(Color.RED,5, SimpleMarkerSymbol.STYLE.CIRCLE));
- graphicsLayer.addGraphic(graphic);
- break;
- case "线":
- Polyline polyline = new Polyline();
- if (pointList.size()>1){
- for(int i=0;i<pointList.size();i++){
- if (i==0){
- polyline.startPath(pointList.get(i));
- }else{
- polyline.lineTo(pointList.get(i));
- }
- }
- }
- graphic = new Graphic(polyline,new SimpleLineSymbol(Color.RED,3, SimpleLineSymbol.STYLE.SOLID));
- graphicsLayer.addGraphic(graphic);
- break;
- case "面":
- Polygon polygon = new Polygon();
- for(int i=0;i<pointList.size();i++){
- if (i==0){
- polygon.startPath(pointList.get(i));
- }else{
- polygon.lineTo(pointList.get(i));
- }
- }
- graphic = new Graphic(polygon,new SimpleFillSymbol(Color.GREEN,SimpleFillSymbol.STYLE.SOLID));
- graphicsLayer.addGraphic(graphic);
- break;
- }
- }
- }
你们的评论、反馈,及对你们有所用,是我整理材料和博文写作的最大的鼓励和唯一动力。欢迎讨论和关注!
没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的自由、好奇、充满创造力的想法被现实的框架所束缚,让创造力自由成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。
没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的自由、好奇、充满创造力的想法被现实的框架所束缚,让创造力自由成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。