ArcGIS API for Android case study 9

Source: Internet
Author: User
Draw geometric objects through interaction

In addition to the function of displaying business data, the client is also responsible for interaction with users. For example, to query a polygon, you must first draw a polygon on the client and then use this polygon to query a space. This function can be learned from the drawgraphicelements example of ArcGIS Android. Let's take a look at the effect of this example first:

Figure 25 draw a polygon through user interaction

The key to this example is that you can perform touch screen operations on the screen, and then record the coordinates of the touch screen to form a geometric object. After the end, the geometric object is drawn to the screen in the form of graphic. Next let's take a look at what happened step by step after clicking the select geometry button above.

First, a dialog box is displayed, which is very simple in Java code:

Geometrybutton. setonclicklistener (NewView. onclicklistener (){

Public VoidOnclick (view v ){

Showdialog (0 );

}

});

Showdialog is a method of activity. This method will display the oncreatedialog method definition dialog box. The content of this method is as follows (some prompts and other code are simplified ):

ProtectedDialog oncreatedialog (IntID ){

Return NewAlertdialog. Builder (drawgraphicelements.This)
. Settitle ("select geometry ")

. Setitems (geometrytypes,NewDialoginterface. onclicklistener (){

Public VoidOnclick (dialoginterface dialog,IntWhich ){

Graphicslayer. Clear ();

String geomtype = geometrytypes [which];

Label. settext (geomtype + "selected .");

Selectedgeometryindex = which;

If(Geomtype. equalsignorecase ("polygon ")){

Simplefillsymbol SFS =NewSimplefillsymbol (color.Green);

SFS. setalpha (60 );

Graphicslayer. setrenderer (NewSimplerenderer (SFS ));

Mylistener. settype ("polygon ");

}Else If(Geomtype. equalsignorecase ("polyline ")){

Graphicslayer. setrenderer (NewSimplerenderer (NewSimplelinesymbol (color.Red, 5 )));

Mylistener. settype ("polyline ");

}Else If(Geomtype. equalsignorecase ("point ")){

Graphicslayer. setrenderer (NewSimplerenderer (NewSimplemarkersymbol (color.Blue, 15, style.Circle)));

Mylistener. settype ("point ");

}

}

}). Create ();

}

This dialog box shows three options through the setitems method: "point", "polyline", and "polygon". When you click a different item, the event listener is triggered, set different draw types based on different items (pay attention to the highlighted lines of code ).

There is also a mylistener object that has not yet appeared. The above goal is to change the painting type by assigning different values to this object. The specific implementation also needs to look at the definition of this mylistener:

Mylistener =NewMytouchlistener (drawgraphicelements.This, Mapview );

Mapview. setontouchlistener (mylistener );

ClassMytouchlistenerExtendsMapontouchlistener {

Multipath poly;

String type = "";

Point startpoint =Null;

PublicMytouchlistener (context, mapview view ){

Super(Context, view );

}

Public VoidSettype (string geometrytype ){

This. Type = geometrytype;

}

PublicString GetType (){

Return This. Type;

}

@ Override

Public BooleanOnsingletap (motionevent e ){

If(Type. Length ()> 1 & type. equalsignorecase ("point ")){

Graphicslayer. Clear ();

Graphic graphic =NewGraphic ();

Graphic. setgeometry (mapview. tomappoint (NewPoint (E. getx (), E. Gety ())));

Graphicslayer. addgraphic (graphic );

Graphicslayer. postinvalidate ();

Clearbutton. setenabled (True);

Return True;

}

Return False;

}

@ Override

Public BooleanOndragpointermove (motionevent from, motionevent ){

If(Type. Length ()> 1 & (type. equalsignorecase ("polyline") |
Type. inclusignorecase ("polygon "))){

Point mappt = mapview. tomappoint (to. getx (), to. Gety ());

If(Startpoint =Null){

Graphicslayer. Clear ();

Poly = type. equalsignorecase ("polyline ")?NewPolyline ():NewPolygon ();

Startpoint = mapview. tomappoint (from. getx (), from. Gety ());

Poly. startpath ((Float) Startpoint. getx (),(Float) Startpoint. Gety ());

Graphic graphic =NewGraphic ();

Graphic. setgeometry (poly );

Graphicslayer. addgraphic (graphic );

}

Poly. lineto ((Float) Mappt. getx (),(Float) Mappt. Gety ());

Graphicslayer. postinvalidate ();

Return True;

}

Return Super. Ondragpointermove (from, );

}

@ Override

Public BooleanOndragpointerup (motionevent from, motionevent ){

If(Type. Length ()> 1 & (type. equalsignorecase ("polyline") |
Type. inclusignorecase ("polygon "))){

If(Type. Repeated signorecase ("polygon ")){

Poly. lineto ((Float) Startpoint. getx (),(Float) Startpoint. Gety ());

}

Startpoint =Null;

Graphicslayer. postinvalidate ();

Clearbutton. setenabled (True);

Return True;

}

Return False;

}

}

If I select "polygon", it is clear that the "type" value in the above code is "polygon". If I perform a touch screen operation on the screen, then the ondragpointermove event is entered. In this event, the screen first captures the first point of the touch, puts the object "startpoint", and creates a new polygon object. After moving a little on the screen, the polygon object will be drawn by calling the lineto method in this event. Finally, the user stops the painting and the program calls the ondragpointerup event listener. The graphic object is constructed completely.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.