Android map development-Overlay application (AMAP)

Source: Internet
Author: User
Tags ontap

 

People who have worked on Android map application development know that the popular map frameworks are Google, Baidu, and AMAP.

For map development on Google and Baidu, you must obtain an api key before being authorized to develop applications. AMAP does not need to be so troublesome. Therefore, this article uses AMAP to develop a simple DEMO.


Before you start, you need to download the corresponding jar development package on the autonavi official website, for: http://api.amap.com/Android/download


First, we will introduce Overlay:

Overlay is a base class that can overwrite the overlay displayed on the top of a map. When an overlay is added, a subclass is derived from this base class, an instance is created, and then added to a list. This list is obtained by calling getOverlays. To allow the user to touch and align a vertex, The subclass should implement the Overlay. Snappable interface.

There is only one constructor, Overlay () ---> Empty Constructor

Key Methods:

Draw (android. graphics. Canvas canvas, MapView mapView, boolean shadow)

OnTap (GeoPoint p, MapView mapView) processes a "click" event.


The following two methods are used to create a small application. Draw an image at a certain point on the map. Process click events. A dialog box is displayed, showing the longitude and latitude of a click.


The specific information is included in the code comment:


First, add the corresponding permissions to the list:


[Html]
<Uses-permission android: name = "android. permission. ACCESS_COARSE_LOCATION"> </uses-permission>
<Uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"> </uses-permission>
<Uses-permission android: name = "android. permission. INTERNET"> </uses-permission>
<Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"> </uses-permission>
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"> </uses-permission>
<Uses-permission android: name = "android. permission. READ_PHONE_STATE"> </uses-permission>
<Uses-permission android: name = "android. permission. CHANGE_WIFI_STATE"> </uses-permission>
<Uses-permission android: name = "android. permission. ACCESS_WIFI_STATE"> </uses-permission>

Then Add the downloaded Map development kit to the project (right-click the project and choose properties> Java Build Path> Libraries> Add external Jars> Find your Map development kit)

The following is the implementation class inherited from the MapActivity class:


[Java]
Package zjut. tsw. overlay;
 
Import java. util. List;
 
Import android. app. AlertDialog;
Import android. content. Context;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. graphics. Canvas;
Import android. graphics. Paint;
Import android. graphics. Point;
Import android. OS. Bundle;
Import android. view. KeyEvent;
Import android. widget. Toast;
 
Import com. amap. mapapi. core. GeoPoint;
Import com. amap. mapapi. map. MapActivity;
Import com. amap. mapapi. map. MapController;
Import com. amap. mapapi. map. MapView;
Import com. amap. mapapi. map. Overlay;
Import com. amap. mapapi. map. Projection;
 
/**
* Overlay of AMAP examples
*
* @ Author tsw
*
*/
Public class MainActivity extends MapActivity {
 
Private MapView mapView;
 
Private MapController mController;
 
 
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
 
MapView = (MapView) findViewById (R. id. mapView); // obtain the MapView object.
 
MapView. setBuiltInZoomControls (true); // enable the zoom control
 
MController = mapView. getController (); // gets the map control object
 
MController. setZoom (10); // sets the initial zoom level.
 
GeoPoint gp = new GeoPoint (int) (30.227123*1E6 ),
(Int) (120.040687*1E6); // locate to the Zhejiang University of Technology. The left parameter is latitude, the right parameter is longitude, and the Unit is microdegree (1 degree = 10 ^ 6 microdegree)
 
MController. setCenter (gp); // set it to map center
 
List <Overlay> list = mapView. getOverlays (); // gets the layer List.
 
DemoOverlay demo = new DemoOverlay (this); // create a custom Overlay
 
List. add (demo); // add a custom layer to the list
}
 
Public class DemoOverlay extends Overlay {
 
Context mContext; // Context
 
Public DemoOverlay (){
Super ();
}
 
Public DemoOverlay (Context c ){
This ();
MContext = c;
}
 
@ Override
Public void draw (Canvas canvas, MapView mapView, boolean shadow ){
 
Projection proj = mapView. getProjection (); // obtain the Projection object
Point mPoint = new Point ();
GeoPoint gp = new GeoPoint (int) (30.227123*1E6 ),
(Int) (120.040687*1E6 ));
Proj. toPixels (gp, mPoint); // converts the longitude and latitude to the pixels on the mobile phone screen and stores them in the Point object.
Paint mPaint = new Paint ();
// MPaint. setColor (Color. RED); // set it to RED.
Bitmap pic = BitmapFactory. decodeResource (getResources (),
R. drawable. da_marker_red); // obtain the Bitmap object.
 
Canvas. drawBitmap (pic, mPoint. x, mPoint. y, mPaint); // plot
Super. draw (canvas, mapView, shadow );
}
 
@ Override
Public boolean onTap (GeoPoint gp, MapView mapView ){
New AlertDialog. Builder (mContext)
. SetTitle ("details ")
. SetMessage (
"Current latitude:" + gp. getLatitudeE6 () * 1.0/(1E6)
+ "\ N longitude:" + gp. getLongitudeE6 () * 1.0
/(1E6). setPositiveButton ("OK", null)
. Create (). show ();
Return super. onTap (gp, mapView );
}
 
}
 
 
}


Add the Map label to the layout file:


[Html]
<Com. amap. mapapi. map. MapView
Android: id = "@ + id/mapView"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: clickable = "true"/>

Put the image resources in the drawable folder.

Related Article

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.