Positioning, covering implementation, and click events of Baidu map in Android 3.5

Source: Internet
Author: User

Positioning, covering implementation, and click events of Baidu map in Android 3.5
Overview some time ago, when using the new version (version 3.5.0) Baidu map, we encountered some setbacks in map covering implementation and adding the click event, I found that a lot of information on the Internet is not detailed enough, so I want to write a blog post about how to implement map locating, map covering, and click events. : Covering and click events:

Implementation of the custom scaling control (note the plus or minus buttons in the figure ):

Steps to implement this routine:

1. Apply for Baidu API_KEY; 2. Download the Baidu SDK and Baidu BS Sample, and import the baidumapi_v3_5_0.jar and locSDK_6.11.jar packages in libs. 3. Create a jniLibs folder under the main directory, import the arm64-v8a, armeabi, armeabi-v7a, x86, x86_64 folder in the so package of the Baidu sdk you decompress, then find the so library arm64-v8a, armeabi, armeabi-v7a, mips, mips64, x86, x86_64 in Baidu BS, if you are under the Baidu BS Sample, you can directly find a sample with all functions and copy all the files in its libs to your libs directory. Each so package here has different functions, you can import these folders in the libs folder.
Note: Structure of my jniLibs package:
4. Add permissions to AndroidManifest:
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
       
       
       
    
   
  
5. Declare your API KEY

  

You can also find their APIs on the Baidu map official website and follow their steps to complete the preparations. Then you can directly view the Demo below.

Demo
Public class BaiduMapActivity extends BaseActivity implements View. OnClickListener {private MapView mMapView; private BaiduMap listener; private LocationClient mLocationClient = null; public BDLocationListener mBDListener = new MyLocationListener (); private List
   
    
BdMapClientList; private double latitude; private double longpolling; private boolean isFirstLocate = true; private Button mButtonAdd; private Button mButtonSub; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); SDKInitializer. initialize (getApplicationContext (); setContentView (R. layout. activity_baidu_map);/*** zoom Map Button */mButtonAdd = (Button) findViewById (R. id. button_add); mButtonSub = (Button) findViewById (R. id. button_sub); mButtonAdd. setOnClickListener (this); mButtonSub. setOnClickListener (this); mMapView = (MapView) findViewById (R. id. mapView_client_raise); mMapView. showZoomControls (false); // The default map scaling control of Baidu map does not display mBaiduMap = mMapView. getMap (); mBaiduMap. setMyLocationEnabled (true); // enable the positioning function of Baidu map mLocationClient = new LocationClient (getApplicationContext (); mLocationClient. registerLocationListener (mBDListener); // registers the map listener initLocation (); mBaiduMap. setOnMarkerClickListener (new BaiduMap. onMarkerClickListener () {@ Override public boolean onMarkerClick (Marker marker) {final String info = (String) marker. getExtraInfo (). get (info); InfoWindow infoWindow; // a Button object is dynamically generated. The user displays InfoWindow final Button textInfo = new Button (getApplicationContext () in the map; textInfo. setBackgroundResource (R. drawable. locate_popup_state); textInfo. setPadding (10, 10, 10, 10); textInfo. setTextColor (Color. BLACK); textInfo. setTextSize (12); textInfo. setText (info); // obtain the longitude and latitude of the clicked cover LatLng ll = marker. getPosition (); textInfo. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {Toast. makeText (BaiduMapActivity. this, you clicked + info, Toast. LENGTH_SHORT ). show () ;}}); // converts the latitude and longitude information of the marker to the coordinate Point p = mBaiduMap on the screen. getProjection (). toScreenLocation (ll); p. y-= 90; LatLng llInfo = mBaiduMap. getProjection (). fromScreenLocation (p); // initialize infoWindow. The last parameter indicates the vertical offset of the display position relative to the covering. Here, you can also input a listener infoWindow = new InfoWindow (textInfo, llInfo, 0); mBaiduMap. showInfoWindow (infoWindow); // display this infoWindow // set the map to center MapStatusUpdate status = MapStatusUpdateFactory. newLatLng (ll); mBaiduMap. setMapStatus (status); return true ;}}); // start locating mLocationClient. start () ;}@ Override public void onClick (View v) {switch (v. getId () {case R. id. button_add: // enlarge the map mBaiduMap. setMapStatus (MapStatusUpdateFactory. zoomIn (); break; case R. id. button_sub: // zoom out the map mBaiduMap. setMapStatus (MapStatusUpdateFactory. zoomOut (); break; default: break;}/*** customizes a location listener for Baidu map, which can be used to monitor the location type, location longitude and latitude changes, and other statuses */private class MyLocationListener implements BDLocationListener {@ Override public void onReceiveLocation (BDLocation bdLocation) {latitude = bdLocation. getLatitude (); // obtain the latitude longpolling = bdLocation. getlongpolling (); // obtain the longitude boolean isLocateFailed = false; // locate if (isFirstLocate) {if (bdLocation. getLocType () = BDLocation. typeGpsLocation) {// GPS Positioning Result ToastUtils. showToast (getApplicationContext (), GPS positioning);} else if (bdLocation. getLocType () = BDLocation. typeNetWorkLocation) {// network location result ToastUtils. showToast (getApplicationContext (), network location);} else if (bdLocation. getLocType () = BDLocation. typeOffLineLocation) {// offline location result ToastUtils. showToast (getApplicationContext (), offline location);} else if (bdLocation. getLocType () = BDLocation. typeServerError | bdLocation. getLocType () = BDLocation. typeNetWorkException | bdLocation. getLocType () = BDLocation. typeCriteriaException) {isLocateFailed = true;}/*** if positioning fails, display the failed dialog */if (isLocateFailed) {simpleDialog ();} initMapDataList (); addOverlay (); isFirstLocate = false ;}}/ *** Method for adding a cover */private void addOverlay () {Marker marker = null; LatLng point = null; markerOptions option = null; BitmapDescriptor bitmap = BitmapDescriptorFactory. fromResource (R. mipmap. customer_location); for (BDMapData data: bdMapClientList) {point = new LatLng (data. getLatitude (), data. getlongpolling (); option = new MarkerOptions (). position (point ). icon (bitmap); marker = (Marker) mBaiduMap. addOverlay (option); // Bundle used for communication Bundle bundle = new Bundle (); bundle. putSerializable (info, data. getName () ++ latitude: + data. getLatitude () + longitude: + data. getlongpolling (); marker. setExtraInfo (bundle); // pass the bundle value to the marker and you can get it when setting a listener for the baiduMap} // move the map to the last flag point MapStatusUpdate status = MapStatusUpdateFactory. newLatLng (point); mBaiduMap. setMapStatus (status);}/*** BaiduAPI routine, initialize and locate */private void initLocation () {LocationClientOption option = new LocationClientOption (); option. setLocationMode (LocationClientOption. locationMode. hight_Accuracy); // optional. The default value is high precision. The positioning mode is set to high precision and low power consumption. Only the device option is used. setCoorType (bd09ll); // optional. The default value is gcj02. Set the returned Positioning Result coordinate system int span = 1000; option. setScanSpan (span); // optional. The default value is 0, that is, only one request is located. Setting the interval between initiating a location request must be greater than or equal to MS is the effective option. setIsNeedAddress (true); // optional. Specifies whether address information is required. option is not required by default. setOpenGps (true); // optional. The default value is false. Set whether to use gps option. setLocationNotify (true); // optional. The default value is false. Set whether to output the gps result at 1 S1 frequency when GPS is valid. option. setIsNeedLocationDescribe (true); // optional. The default value is false. You can set whether location semantic results are required in BDLocation. getLocationDescribe. The result is similar to "near Tiananmen Square, Beijing" option. setIsNeedLocationPoiList (true); // optional. The default value is false. You can set whether POI results are required in BDLocation. get option in getPoiList. setIgnoreKillProcess (false); // optional. The default value is false. This parameter indicates that the SDK contains a SERVICE and is released to an independent process. This parameter indicates whether to kill the process during stop. By default, the option is killed. setIgnoreCacheException (false); // optional. The default value is false. Set whether to collect CRASH information. The default value is "option. setEnableSimulateGps (false); // optional. The default value is false. It specifies whether to filter the gps simulation results. mLocationClient is required by default. setLocOption (option);} // initialize the private void initMapDataList () {bdMapClientList = new ArrayList <> (); // make the longitude and latitude of all the covers close to your own longitude and latitude, so that you can open the map to see those covers bdMapClientList. add (new BDMapData (ZTE skyworth, latitude-0.0656, longpolling-0.00354); bdMapClientList. add (new BDMapData (lingzhuo technology, latitude + 0.024, longpolling-0.0105); bdMapClientList. add (new BDMapData (lanxiang Driving School, latitude-0.00052, longpolling-0.01086); bdMapClientList. add (new BDMapData (Uniqlo discount store, latitude + 0.0124, longwall + 0.00184);}/*** method of initializing the dialog displayed when locating failure */public void simpleDialog () {AlertDialog. builder builder = new AlertDialog. builder (BaiduMapActivity. this); builder. setMessage (the current network is unavailable. Check the network settings ). setNeutralButton (OK, new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {dialog. dismiss () ;}}); AlertDialog dialog = builder. create (); dialog. setCanceledOnTouchOutside (true); dialog. show ();}}
   
Classes used to store cover information
Public class BDMapData {private String name; private double latitude; // latitude private double longpolling; // longitude public BDMapData (String name, double latitude, double longpolling) {this. name = name; this. latitude = latitude; this. longpolling = longpolling;} public String getName () {return name;} public void setName (String name) {this. name = name;} public double getLatitude () {return latitude;} public void setLatitude (double latitude) {this. latitude = latitude;} public double getlongpolling () {return longpolling;} public void setlongpolling (double longpolling) {this. longpolling = longpolling ;}}
Layout file activity_baidu_map:

   
       
            
     
                  
        
         
          
           
           
           
          
         
        
       
      
     
    
               
I don't want to post source code... Content in the drawable Folder:

Item_bg.xml


   
       
        
        
    
   

Add_state.xml


   
       
        
    
   

Sub_state.xml:


   
       
        
    
   

Locate_popup.xml
White: # ffffff
Gray: # d0d0d0


   
       
        
    
   
Image in mipmap

Customer_location.png:
Add_group.png:
Add_group_press.png:
Btn_reduction_group.png:
Btn_reduction_group_press.png:
Btn_back_nZ restart? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcm1hbC5wbmc6PGltZyBhbHQ9" here write picture description "src =" http://www.bkjia.com/uploads/allimg/151030/041K22337-8.png "title =" \ "/>

Summary: Here we only implement the most basic BaiduMap functions. It also has many other functions, but this function is not frequently used, some applications such as Meituan use cover. As for Baidu sdks, you can write a blog to show them.

 

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.