Android Baidu map basic implementation (TAG + GPS)

Source: Internet
Author: User

[Java] package com. baidu. map; import java. util. arrayList; import java. util. list; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. point; import android. graphics. drawable. drawable; import android. location. location; import android. location. locationManager; import android. OS. bundle; import android. widg Et. toast; import com. baidu. mapapi. BMapManager; import com. baidu. mapapi. geoPoint; import com. baidu. mapapi. itemizedOverlay; import com. baidu. mapapi. locationListener; import com. baidu. mapapi. MKAddrInfo; import com. baidu. mapapi. MKDrivingRouteResult; import com. baidu. mapapi. MKPoiResult; import com. baidu. mapapi. MKSearchListener; import com. baidu. mapapi. MKTransitRouteResult; import com. baidu. mapapi. MKWal KingRouteResult; import com. baidu. mapapi. mapActivity; import com. baidu. mapapi. mapController; import com. baidu. mapapi. mapView; import com. baidu. mapapi. myLocationOverlay; import com. baidu. mapapi. overlayItem; import com. baidu. mapapi. poiOverlay; import com. baidu. mapapi. projection; public class BmapActivity extends MapActivity {// defines the map engine management class private BMapManager mapManager; // defines the search service class private MapView map View; private MapController mapController; LocationListener mLocationListener = null; // register this listener when onResume is used. Remove MyLocationOverlay mLocationOverlay = null when onPause is used; // locate the layer private double dLat; private double dLon; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // initialize MapActivity mapManager = new BMapManager (getAppl Ication (); // The first parameter of the init method must be filled in with the applied APIKey mapManager. init ("DE7BC8B377AFA895ED0C16F504B5C4FC5E3E149B", null); super. initMapActivity (mapManager); mapView = (MapView) findViewById (R. id. map_View); // set the map mode to traffic map // mapView. setTraffic (true); // set to enable the built-in zoom control mapView. setBuiltInZoomControls (true); mapView. displayZoomControls (true); // set overlay to be displayed during the scaling animation. mapView is not drawn by default. setDrawOverlayWhenZooming (true); mapCo Ntroller = mapView. getController (); mapController. setZoom (16); // locate myself (); // Add the positioning layer mLocationOverlay = new MyLocationOverlay (this, mapView); mapView. getOverlays (). add (mLocationOverlay); Drawable marker = getResources (). getDrawable (R. drawable. da_marker_red); marker. setBounds (0, 0, marker. getIntrinsicWidth (), marker. getIntrinsicHeight (); // Intrinsic inherent mapView. getOverlays (). add (new MyItemiz EdOverlay (marker, this);} // draw class MyItemizedOverlay extends ItemizedOverlay <OverlayItem> {// attribute private Drawable marker; private Context mContext; private List <OverlayItem> geoList = new ArrayList <OverlayItem> (); // constructor public MyItemizedOverlay (Drawable marker, Context context) {super (boundCenterBottom (marker); this. marker = marker; this. mContext = context; // construct the geographic coordinate GeoPoint p1 = New GeoPoint (int) (dLat * 1E6), (int) (dLon * 1E6); geoList. add (new OverlayItem (p1, "P1", "This is my current location"); populate (); // fill execution method} // draw method public void draw (Canvas canvas, MapView mapView, boolean shadow) {// projection, used to convert the pixel Coordinate System on the screen and the coordinate system of the earth's longitude and latitude points. Projection projection = mapView. getProjection (); for (int index = size ()-1; index> = 0; index --) {OverlayItem overlayItem = this. getItem (index); String title = OverlayItem. getTitle (); Point point = projection. toPixels (overlayItem. getPoint (), null); Paint painttext = new Paint (); painttext. setColor (Color. BLACK); painttext. setTextSize (15); canvas. drawText (title, point. x-30, point. y-25, painttext);} super. draw (canvas, mapView, shadow); boundCenterBottom (marker);} // Add member method @ Override protected OverlayItem createItem (int I) {return geoList. get (I );}@ Override public int size () {return geoList. size () ;}// Add the Click Event public boolean onTap (int I) {setFocus (geoList. get (I); Toast. makeText (this. mContext, geoList. get (I ). getSnippet (), Toast. LENGTH_LONG ). show (); // return true;} public boolean onTap (GeoPoint point, MapView mapView) {return super. onTap (point, mapView) ;}// locate your location public void myself () {LocationManager lm = (LocationManage R) getSystemService (LOCATION_SERVICE); Location l = lm. getLastKnownLocation (LocationManager. GPS_PROVIDER); if (l = null) {Toast. makeText (BmapActivity. this, "unable to get your location", Toast. LENGTH_SHORT ). show ();/* default position */dLat = 34.824289; dLon = 113.689044; GeoPoint geoPoint = new GeoPoint (int) (dLat * 1e6), (int) (dLon * 1e6); mapController. setCenter (geoPoint);} else {dLat = (int) (l. getLatitude ()* 1E6); dLon = (int) (l. getlongpolling () * 1E6) ;}@ Override protected boolean isRouteDisplayed () {return false ;}@ Override protected void onDestroy () {if (mapManager! = Null) {// call this method mapManager before exiting the program. destroy (); mapManager = null;} super. onDestroy () ;}@ Override protected void onPause () {if (mapManager! = Null) {// terminate Baidu map API mapManager. getLocationManager (). removeUpdates (mLocationListener); mLocationOverlay. disableMyLocation (); mLocationOverlay. disableCompass (); // disable the compass mapManager. stop ();} super. onPause () ;}@ Override protected void onResume () {if (mapManager! = Null) {// enable the Baidu map API // register the positioning event, and then move the map to the positioning point mapManager. getLocationManager (). requestLocationUpdates (mLocationListener); mLocationOverlay. enableMyLocation (); mLocationOverlay. enableCompass (); // open the compass mapManager. start ();} super. onResume ();}/*** implements the MKSearchListener interface, used to Implement Asynchronous search service ** @ author liufeng */public class MySearchListener implements MKSearchListener {/*** search address information result based on latitude and longitude ** @ param result * search result * @ param iError * error number (0 indicates a correct return) */public void onGetAddrResult (MKAddrInfo result, int iError) {}/*** driving route search result ** @ param result * search result * @ param iError * error code */public void onGetDrivingRouteResult (MKDrivingRouteResult result, int iError) {}/***** POI search results (range search, city POI search, and peripheral search) * @ param result search result ** @ param type * return result type, 21: poi list 7: City list) * @ param iError * error code (0 indicates correct return) */public void onGetPoiResult (MKPoiResult result, int type, int iError) {if (result = null) {return;} // PoiOverlay is the Overlay PoiOverlay (BmapActivity) provided by the baidu map api for displaying POI. this, mapView); // you can specify POI data poioverlay. setData (result. getAllPoi (); // display PoiOverlay on the map (mark the searched point of interest on the map) mapView. getOverlays (). add (poioverlay);}/*** bus transfer Route Search result ** @ param result search result *** @ param iError * error code (0 indicates a correct response) **/public void onGetTransitRouteResult (MKTransitRouteResult result, int iError) {}/***** walking Route Search result *** @ param result * search result ** @ param iError * error code (0 indicates a correct response) **/public void onGetWalkingRouteResult (MKWalkingRouteResult result, int iError ){}}}

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.