Android positioning and map development instances and android positioning instances
In android development, maps and positioning are an indispensable part of many software programs. These special features also bring a lot of convenience to people.
First, we will introduce the main categories in the map package:
MapController: mainly controls map movement and scaling. It takes a GPS coordinate as the center, controls the view component in MapView, manages Overlay, and provides basic View functions. Use multiple Map modes (Map mode (some cities can update traffic conditions in real time), satellite mode, street view mode) to view Google Map. Common Methods: animateTo (GeoPoint point) setCenter (GeoPoint point) setZoom (int zoomLevel.
Mapview: A view used to display a map. It is derived 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 and satellite mode. The setSatellite (boolean) setTraffic (boolean) and setStreetView (boolean) methods are used.
Overlay: Overlay is the top layer of MapView. You can extend its ondraw interface to display some of your own items in MapView. MapView manages Overlay through MapView. getOverlays.
Projection: the conversion between GPS coordinates and device coordinates (GeoPoint and Point) in MapView ).
Locate the main categories in the system package:
LocationManager: This class provides the ability to access the location service and to obtain the best location provider. In addition, the near alert function can also be implemented using this class.
LocationProvider: this class is the abstract class of the positioning provider. The location provider can periodically report the geographical location of devices.
LocationListener: Provides the callback function when the location information changes. You must register the listener object in the locating manager in advance.
Criteria: This class enables the application to select an appropriate location provider by setting attributes in the LocationProvider.
Geocoder: class used to process geocode and reverse geocode. Geocoding refers to converting an address or other description into a longitude or latitude. Reverse geocoding refers to converting a longitude or latitude to an address or a description language, which contains two constructors, the coordinates of the longitude and latitude must be input. The getFromLocation method can obtain an array of addresses.
The following describes how to develop a map positioning instance. Before developing a map, you need to obtain a lot of information on the Android map API key. I will not repeat it here.
First, you must set the corresponding 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 in 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"
/>
The following is the core code. I have commented on the important points:
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 the traffic mode
MapView. setTraffic (true );
// Set the satellite mode
MapView. setSatellite (false );
// Set the Street View Mode
MapView. setStreetView (false );
// Set scaling Control
MapView. setBuiltInZoomControls (true );
MapView. setClickable (true );
MapView. setEnabled (true );
// Obtain the MapController instance
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 );
// Criteria with the best effect
String provider = locationManager. getBestProvider (criteria, true );
// Obtain the Location
Location location = locationManager. getLastKnownLocation (provider );
UpdateWithLocation (location );
// Register a periodic update once every 3 seconds
LocationManager. requestLocationUpdates (provider, 3000, 0, locationListener );
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// TODO Auto-generated method stub
Menu. add (0, 1, 1, "traffic mode ");
Menu. add (0, 2, "satellite mode ");
Menu. add (, 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 mode
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. getlongdistance () * 1E6 ));
// Locate the specified Coordinate
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
}
// Start this function 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 ();
// Change the longitude and latitude to the coordinates of the actual screen.
GeoPoint geoPoint = new GeoPoint (int) (location. getLatitude () * 1E6), (int) (location. getlongdistance () * 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 image to the corresponding position.
Canvas. drawBitmap (bmp, myScreen. x, myScreen. y, paint );
Canvas. drawText ("Heaven has no path", 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 exit? ")
. 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 ("return ",
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, let's take a look at the effect after running:
You can zoom in and out:
However, you can use the menu key to switch between different modes:
The above is switched to the satellite mode. Because a map consumes a lot of network resources, if the network is slow, it will wait for a long time.
Android development, based on Baidu map Positioning
An Android Baidu mobile map Manual (automatic) Locating instance
GPS basic sample code
+ When Baidu map calls an API, you must first become a member of Baidu developers and apply for an AppKey/ID.
Network Access programming to obtain the specified longitude and latitude map image
GPS Positioning of the latitude and longitude, marked to display in the image, such as self-Drawing
No source code
See if anyone has done it and can send it to you.