In the development of Android, maps and positioning are the indispensable content of many software, these features also bring a lot of convenience to people.
First introduce the main classes in the map package:
Mapcontroller: Main control map Movement, telescopic, with a GPS coordinates as the center, control the view components in the Mapview, management overlay, provide the basic function of the view. Use multiple map modes (map mode (some cities can update traffic in real time), satellite mode, Street View mode) to see Google Maps. Common methods: Animateto (GeoPoint point) setcenter (GeoPoint point) setzoom (int zoomlevel) and so on.
Mapview: Is the view used to display the map, which derives from Android.view.ViewGroup. When Mapview gets the focus, you can control the movement and scaling of the map. Maps can be displayed in different forms, such as Street View mode, satellite mode, etc., via the Setsatellite (Boolean) Settraffic (Boolean), Setstreetview (Boolean) method.
Overlay: is covering to the top of Mapview, you can extend its OnDraw interface, customize the display of something in Mapview. Mapview management of Overlay through Mapview.getoverlays ().
The conversion of GPS coordinates to device coordinates (Geopoint and point) in Projection:mapview.
Locate the main class in the System package:
Locationmanager: This class provides access to location services and provides the ability to get the best location providers. In addition, the proximity alert feature can be implemented with this class.
Locationprovider: This class is an abstract class for locating providers. The location provider has the ability to periodically report the location of the device.
Locationlistener: Provides a callback function when the location information changes. Listener objects must be registered in the location manager beforehand.
Criteria: This class allows the application to select the appropriate location provider by the properties set in the Locationprovider.
Geocoder: A class for working with geocoding and reverse geocoding. Geocoding refers to the conversion of addresses or other descriptions into longitude and latitude, while reverse geocoding translates longitude and latitude into addresses or description languages, which contain two constructors that require the coordinates of longitude and latitude to be passed in. The Getfromlocation method can get a set of arrays of addresses.
The following is the beginning of the development of map positioning instances, before the development of maps need to get the Android map API key Online There is a lot of information, here will not repeat.
The first thing to do is to set the appropriate permissions and maps library in Manifest.xml:
<application< p= "" >
android:icon= "@drawable/ic_launcher"
Android:label= "@string/app_name" >
<activity< p= "" >
Android:label= "@string/app_name"
Android:name= ". Mymapactivity ">
Main.xml under Layout:
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >
<com.google.android.maps.mapview< p= "" >
Android:id= "@+id/mapview"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:apikey= "008UU0X2A7GWLK2LZCW872AFBAPLHJ-U2R26WGW"
/>
Here is the core code, the important place I made the comment:
public class Mymapactivity extends Mapactivity {
/** called when the activity is first created. */
Private Mapcontroller Mapcontroller;
Private Mapview Mapview;
Private Myoverlay Myoverlay;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Locationmanager locationmanager= (Locationmanager) Getsystemservice (Context.location_service);
mapview= (Mapview) This.findviewbyid (R.id.mapview);
Set up traffic mode
Mapview.settraffic (TRUE);
Set up satellite mode
Mapview.setsatellite (FALSE);
Set Street View mode
Mapview.setstreetview (FALSE);
Setting the Zoom control
Mapview.setbuiltinzoomcontrols (TRUE);
Mapview.setclickable (TRUE);
Mapview.setenabled (TRUE);
Get Mapcontroller instances
Mapcontroller=mapview.getcontroller ();
Mapcontroller.setzoom (15);
Myoverlay=new Myoverlay ();
List overlays=mapview.getoverlays ();
Overlays.add (Myoverlay);
Criteria criteria=new criteria ();
Criteria.setaccuracy (Criteria.accuracy_fine);
Criteria.setaltituderequired (FALSE);
Criteria.setbearingrequired (FALSE);
Criteria.setcostallowed (FALSE);
Criteria.setpowerrequirement (Criteria.power_low);
The best criteria for achieving results
String Provider=locationmanager.getbestprovider (criteria, true);
Get location
Location Location=locationmanager.getlastknownlocation (provider);
Updatewithlocation (location);
Register for a recurring update, 3 seconds at a time
Locationmanager.requestlocationupdates (provider, 0, Locationlistener);
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
TODO auto-generated Method Stub
Menu.add (0, 1, 1, "traffic mode");
Menu.add (0,2,2, "satellite mode");
Menu.add (0,3,3, "Street View Mode");
return Super.oncreateoptionsmenu (menu);
}
@Override
public boolean onoptionsitemselected (MenuItem item) {
TODO auto-generated Method Stub
super.onoptionsitemselected (item);
Switch (Item.getitemid ()) {
Case 1://Traffic Mode
Mapview.settraffic (TRUE);
Mapview.setsatellite (FALSE);
Mapview.setstreetview (FALSE);
Break
Case 2://Satellite Model
Mapview.setsatellite (TRUE);
Mapview.setstreetview (FALSE);
Mapview.settraffic (FALSE);
Break
Case 3://Street View mode
Mapview.setstreetview (TRUE);
Mapview.settraffic (FALSE);
Mapview.setsatellite (FALSE);
Break
Default
Mapview.settraffic (TRUE);
Mapview.setsatellite (FALSE);
Mapview.setstreetview (FALSE);
Break
}
return true;
}
private void Updatewithlocation (location location) {
if (location!=null) {
Set coordinates for the drawing class
Myoverlay.setlocation (location);
GeoPoint geopoint=new GeoPoint ((int) (Location.getlatitude () *1e6), (int) (Location.getlongitude () *1e6));
Navigates to the specified coordinates
Mapcontroller.animateto (GeoPoint);
Mapcontroller.setzoom (15);
}
}
Private final Locationlistener locationlistener=new Locationlistener () {
@Override
public void onstatuschanged (String provider, int status, Bundle extras) {
TODO auto-generated Method Stub
}
@Override
public void onproviderenabled (String provider) {
TODO auto-generated Method Stub
}
@Override
public void onproviderdisabled (String provider) {
TODO auto-generated Method Stub
}
This function starts when the coordinates change
@Override
public void onlocationchanged (location location) {
TODO auto-generated Method Stub
Updatewithlocation (location);
}
};
Class Myoverlay extends overlay{
Private location location;
public void setlocation (location location) {
This.location=location;
}
@Override
public boolean draw (canvas canvas, Mapview Mapview, Boolean shadow,
Long when) {
TODO auto-generated Method Stub
Super.draw (canvas, Mapview, shadow);
Paint paint=new paint ();
Point Myscreen=new Point ();
Replace the latitude and longitude with the coordinates of the actual screen.
GeoPoint geopoint=new GeoPoint ((int) (Location.getlatitude () *1e6), (int) (Location.getlongitude () *1e6));
Mapview.getprojection (). Topixels (GeoPoint, Myscreen);
Paint.setstrokewidth (1);
Paint.setargb (255, 255, 0, 0);
Paint.setstyle (Paint.Style.STROKE);
Bitmap Bmp=bitmapfactory.decoderesource (Getresources (), r.drawable.mypicture);
Draw the picture to the appropriate position.
Canvas.drawbitmap (BMP, Myscreen.x, myscreen.y,paint);
Canvas.drawtext ("Paradise Without Road", myscreen.x, MYSCREEN.Y, paint);
return true;
}
}
@Override
Protected Boolean isroutedisplayed () {
TODO auto-generated Method Stub
return false;
}
@Override
public boolean onKeyDown (int keycode, keyevent event) {
TODO auto-generated Method Stub
if (keycode = = Keyevent.keycode_back) {
Alertdialog.builder Builder = new Alertdialog.builder (this);
Builder.setmessage ("Are you sure you want to quit?")
. Setcancelable (False)
. Setpositivebutton ("OK",
New Dialoginterface.onclicklistener () {
public void OnClick (Dialoginterface dialog,
int id) {
MyMapActivity.this.finish ();
Android.os.Process
. KillProcess (android.os.Process
. Mypid ());
Android.os.Process.killProcess (Android.os.Process.myTid ());
Android.os.Process.killProcess (Android.os.Process.myUid ());
}
})
. Setnegativebutton ("Back",
New Dialoginterface.onclicklistener () {
public void OnClick (Dialoginterface dialog,
int id) {
Dialog.cancel ();
}
});
Alertdialog alert = Builder.create ();
Alert.show ();
return true;
}
Return Super.onkeydown (KeyCode, event);
}
}
Next look at the post-run effect:
To zoom in and out:
However, use the menu key to switch between different modes:
The above is switched to satellite mode. Because maps need to consume a lot of network resources, if the network is slow, it will wait a long time.
Android location and map development examples