Navigation and touch screen operations
Now let's take a look at the operations on mapview. For example, the first problem is map navigation. Now, let's create a new ArcGIS Android project, add a mapview, and add a map service (similar to hello World ).
After running this program, you will be able to see this map service immediately. At this time, if I want to drag the map, you can drag it with the mouse; if I want to zoom in, double-click it on the simulator. In fact, if it is on a real machine, you can also use two fingers to make a zoom-In gesture on the screen, but it is troublesome to simulate zoom-in on the simulator. So here I want to add two buttons: "zoom in" and "zoom out ":
"@ + ID/buttonzoomin" Android: text ="Zoom in"
Android: layout_width ="Wrap_content"Android: layout_height ="Wrap_content"
Android: layout_alignparentleft ="True"/>
"@ + ID/buttonzoomout" Android: text ="Small"
Android: layout_width ="Wrap_content"Android: layout_height ="Wrap_content"
Android: layout_alignparentright ="True"/>
Public ClassNavigationExtendsActivity {
PrivateMapview map =Null;
PrivateButton buttonzoomin =Null;
PrivateButton buttonzoomout =Null;
Public VoidOncreate (bundle savedinstancestate ){
Super. Oncreate (savedinstancestate );
Setcontentview (R. layout.Main);
This. Map = (mapview) findviewbyid (R. Id.Map);
This. Buttonzoomin = (button) findviewbyid (R. Id.Buttonzoomin);
This. Buttonzoomout = (button) findviewbyid (R. Id.Buttonzoomout);
This. Buttonzoomin. setonclicklistener (NewOnclicklistener (){
Public VoidOnclick (view v ){
Navigation.This. Map. zoomin ();
}
});
This. Buttonzoomout. setonclicklistener (NewOnclicklistener (){
Public VoidOnclick (view v ){
Navigation.This. Map. zoomout ();
}
});
}
}
Yes, you can call the zoomin and zoomout methods of mapview. In this way, you can run this program as shown in Figure 20, by using two buttons and dragging on the map, you can scale the map to your desired position.
Figure 20 zoom in and out a map
However, we know there are other operations on the touch screen, such as "Long press ". What should I do if my current program wants users to get the coordinates of a point on the map through a "Long press" operation and then perform subsequent tagging and other operations?
In the ArcGIS Android API, an event listening type is onlongpresslistener. Through this event listening, mapview can respond to users' long-press events, now we add a long-pressed listener to this mapview and display the map coordinates of the touch area in the text box:
"@ + ID/label"
Android: layout_width ="Wrap_content" Android: layout_height ="Wrap_content"
Android: textcolor ="# Ffffff" Android: layout_centerhorizontal ="True"
Android: layout_alignparentbottom ="True"/>
This. Map. setonlongpresslistener (New Onlongpresslistener (){
Public VoidOnlongpress (FloatX,FloatY ){
Point Pt = navigation.This. Map. tomappoint (x, y );
Navigation.This. Label. settext ("X:" + Pt. getx () + "Y:" + Pt. Gety ());
}
});
Run the program and then use the mouse to simulate a long-pressed operation on the simulator. You can see that the current coordinate value is displayed in the text box at the bottom, 21:
Figure 21. The coordinate value is displayed after the screen is pressed.
Mapview supports the following types of events:
Event listening |
Trigger Condition |
Onlongpresslistener |
Long time on mapview |
Onmapextentchangedlistener |
When mapview's map range is changed |
Onpanlistener |
When dragging a map in mapview |
Onpinchlistener |
When two scaling operations are performed on mapview |
Onsingletaplistener |
When you click |
Onstatuschangedlistener |
When the mapview status changes, such as when it is created or initialized |
Onzoomlistener |
When mapview is scaled |
Mapontouchlistener |
When you press and hold on mapview and move (similar to a drawing Operation) |