Use Baidu map Android sdk's high imitation WeChat sending location function

Source: Internet
Author: User

Use Baidu map Android sdk's high-imitation sending location function

I have been in touch with the Baidu map development platform for half a month. These two days I tried to imitate the location sending function to my friends, and the operation capability on Baidu maps has taken another step.

(If you need a complete demo, leave a comment to your mailbox)

 

I encountered some difficulties when implementing this function, which may also be difficult for others.

 

1. In the sending function, no matter how you drag a map, there is always a cover that is fixed in the center of the MapView. How can this problem be achieved?

In fact, this is very easy to implement. As long as an ImageView is covered in the layout file of the MapView, no matter how you drag the map, the covering (ImageView) is always fixed to the central position of the MapView.

 

2. How can I obtain the geographical coordinates in the center of the MapView, that is, the coordinates at the screen covering?

To obtain the geographical coordinates in the center of the MapView, first obtain the physical coordinates of the covering on the screen. here we need to distinguish the geographical coordinates (longitude and latitude) from the physical coordinates (xy axis );

You can use

[Java]View plaincopy
  1. To obtain the physical coordinates. The physical coordinates remain unchanged, while the geographical coordinates corresponding to the physical coordinates change as you drag the map.

     

     

    [Java]View plaincopy
    1. CurrentLatLng = mBaiduMap. getProjection (). fromScreenLocation (
    2. MCenterPoint );.

       

       

      3. How to obtain all the poi information (that is, the information of the surrounding buildings) around the geographical coordinate, not just a certain type of point of interest?

      In order to achieve this function, it really took me a lot of effort, because I used to think that I was using POI to search for the surrounding area. Someone suggested that I could use the circular axis to search for different keywords, but it is really difficult to achieve that effect. By checking the API, we can find that this problem can be solved through anti-geographic code. For details about anti-geographic code, visit the Baidu map official website and call it, returns a list of buildings near the geographic coordinate.

       

      4. When you drag a map, how can I update the list to the surrounding information of the current central geographic location?

      This difficulty is not difficult to implement, but it is only to rewrite mBaiduMap. setOnMapTouchListener (touchListener); touch events, in the callback function of the touch listener

      Convert the physical coordinate coordinates in the center of the MapView to the corresponding geographical coordinate, and then obtain the surrounding information through anti-Geographic Encoding. The truth is the same as problem 3.

       

      5. When you click a specific item in listview, how does one display the geographical position of the item in MapView, that is, in the center of the screen?

      You can use the listview adapter to obtain the position information of the item, including the longitude and latitude, and then use an animation to jump to the center of the screen.

       

      I think the above five problems are the key to implementing this function. below is (if you need a complete demo, Please comment and leave a mailbox)

      The above is a MapVIew, the covering is fixed in the center, and the white square in the lower left corner of the map is the button to return to the positioning point, because there is no good-looking picture, it is left blank

      The following is a listView that displays some location information around the location indicated by the map.

       

      Paste the core code to implement this function

      Activity Type

       

      [Java]View plaincopy
      1. Package com. vr. souhuodong. UI. Sou;
      2.  
      3. Import java. util. ArrayList;
      4. Import java. util. List;
      5.  
      6. Import android. app. Activity;
      7. Import android. content. Intent;
      8. Import android. graphics. Point;
      9. Import android.net. Uri;
      10. Import android. OS. Bundle;
      11. Import android. view. MotionEvent;
      12. Import android. view. View;
      13. Import android. widget. AdapterView;
      14. Import android. widget. AdapterView. OnItemClickListener;
      15. Import android. widget. ImageView;
      16. Import android. widget. ListView;
      17. Import android. widget. ProgressBar;
      18.  
      19. Import com. baidu. location. BDLocation;
      20. Import com. baidu. location. BDLocationListener;
      21. Import com. baidu. location. LocationClient;
      22. Import com. baidu. location. LocationClientOption;
      23. Import com. baidu. mapapi. map. BaiduMap;
      24. Import com. baidu. mapapi. map. BaiduMap. OnMapTouchListener;
      25. Import com. baidu. mapapi. map. BitmapDescriptor;
      26. Import com. baidu. mapapi. map. BitmapDescriptorFactory;
      27. Import com. baidu. mapapi. map. MapStatusUpdate;
      28. Import com. baidu. mapapi. map. MapStatusUpdateFactory;
      29. Import com. baidu. mapapi. map. MapView;
      30. Import com. baidu. mapapi. map. MarkerOptions;
      31. Import com. baidu. mapapi. map. MyLocationConfiguration;
      32. Import com. baidu. mapapi. map. MyLocationConfiguration. LocationMode;
      33. Import com. baidu. mapapi. map. MyLocationData;
      34. Import com. baidu. mapapi. map. OverlayOptions;
      35. Import com. baidu. mapapi. model. LatLng;
      36. Import com. baidu. mapapi. search. core. PoiInfo;
      37. Import com. baidu. mapapi. search. core. SearchResult;
      38. Import com. baidu. mapapi. search. geocode. GeoCodeResult;
      39. Import com. baidu. mapapi. search. geocode. GeoCoder;
      40. Import com. baidu. mapapi. search. geocode. OnGetGeoCoderResultListener;
      41. Import com. baidu. mapapi. search. geocode. ReverseGeoCodeOption;
      42. Import com. baidu. mapapi. search. geocode. ReverseGeoCodeResult;
      43. Import com. vr. souhuodong. R;
      44. Import com. vr. souhuodong. UI. Adapter. PlaceListAdapter;
      45.  
      46. Public class ChoosePlaceActivity extends Activity {
      47.  
      48. MapView mMapView;
      49. BaiduMap mBaiduMap;
      50. ProgressBar mLoadBar;
      51. ImageView mSelectImg;
      52.  
      53. // Locate
      54. LocationClient mLocationClient = null;
      55. MyBDLocationListner mListner = null;
      56. BitmapDescriptor mCurrentMarker = null;
      57.  
      58. // Current longitude and latitude
      59. Double mLantitude;
      60. Double mLongtitude;
      61. LatLng mLoactionLatLng;
      62.  
      63. // Set the marker for the first time
      64. Boolean isFirstLoc = true;
      65.  
      66. // Screen coordinates of the MapView Center
      67. Point mCenterPoint = null;
      68.  
      69. // Geocode
      70. GeoCoder mGeoCoder = null;
      71.  
      72. // Location list
      73. ListView mListView;
      74. PlaceListAdapter mAdapter;
      75. List MInfoList;
      76. PoiInfo mCurentInfo;
      77.  
      78. @ Override
      79. Protected void onCreate (Bundle savedInstanceState ){
      80. // TODO Auto-generated method stub
      81. Super. onCreate (savedInstanceState );
      82. SetContentView (R. layout. activity_chooseplace );
      83.  
      84. InitView ();
      85. }
      86.  
      87. /**
      88. * Initialization Interface
      89. */
      90. Private void initView (){
      91. // TODO Auto-generated method stub
      92. // Initialize the map
      93. MMapView = (MapView) findViewById (R. id. chooseplace_bmapView );
      94. MMapView. showZoomControls (false );
      95. MBaiduMap = mMapView. getMap ();
      96. MapStatusUpdate msu = MapStatusUpdateFactory. zoomTo (17.0f );
      97. MBaiduMap. setMapStatus (msu );
      98. MBaiduMap. setOnMapTouchListener (touchListener );
      99.  
      100. // Initialize the POI Information List
      101. MInfoList = new ArrayList ();
      102.  
      103. // Initialize the screen coordinates of the current MapView center and the current geographic coordinates
      104. MCenterPoint = mBaiduMap.getMapStatus().tar getScreen;
      105. MLoactionLatLng = mBaiduMap.getMapStatus().tar get;
      106.  
      107. // Locate
      108. MBaiduMap. setMyLocationEnabled (true );
      109. MLocationClient = new LocationClient (this );
      110. MListner = new MyBDLocationListner ();
      111. MLocationClient. registerLocationListener (mListner );
      112. LocationClientOption option = new LocationClientOption ();
      113. Option. setOpenGps (true); // enable gps
      114. Option. setCoorType ("bd09ll"); // sets the coordinate type.
      115. Option. setScanSpan (1000 );
      116. MLocationClient. setLocOption (option );
      117. MLocationClient. start ();
      118.  
      119. // Geocode
      120. MGeoCoder = GeoCoder. newInstance ();
      121. MGeoCoder. setOnGetGeoCodeResultListener (GeoListener );
      122.  
      123. // List of surrounding locations
      124. MListView = (ListView) findViewById (R. id. place_list );
      125. MLoadBar = (ProgressBar) findViewById (R. id. place_progressBar );
      126. MListView. setOnItemClickListener (itemClickListener );
      127. MAdapter = new PlaceListAdapter (getLayoutInflater (), mInfoList );
      128. MListView. setAdapter (mAdapter );
      129.  
      130. MSelectImg = new ImageView (this );
      131. }
      132.  
      133. Public void turnBack (View view ){
      134. // Implement animation jump
      135. MapStatusUpdate u = MapStatusUpdateFactory. newLatLng (mLoactionLatLng );
      136. MBaiduMap. animateMapStatus (u );
      137.  
      138. MBaiduMap. clear ();
      139. // Initiate anti-geographic code search
      140. MGeoCoder. reverseGeoCode (new ReverseGeoCodeOption ())
      141. . Location (mLoactionLatLng ));
      142.  
      143. }
      144.  
      145. @ Override
      146. Protected void onDestroy (){
      147. // TODO Auto-generated method stub
      148. Super. onDestroy ();
      149. MLocationClient. stop ();
      150. MGeoCoder. destroy ();
      151. }
      152.  
      153. // Locate the listener
      154. Private class MyBDLocationListner implements BDLocationListener {
      155.  
      156. @ Override
      157. Public void onReceiveLocation (BDLocation location ){
      158. // TODO Auto-generated method stub
      159. // After map view is destroyed, it is not processed in the new receiving location
      160. If (location = null | mMapView = null)
      161. Return;
      162. MyLocationData data = new MyLocationData. Builder ()//
      163. //. Direction (mCurrentX )//
      164. . Accuracy (location. getRadius ())//
      165. . Latitude (location. getLatitude ())//
      166. . Longpolling (location. getlongpolling ())//
      167. . Build ();
      168. MBaiduMap. setMyLocationData (data );
      169. // Set the custom icon
      170. MyLocationConfiguration config = new MyLocationConfiguration (
      171. LocationMode. NORMAL, true, null );
      172. MBaiduMap. setMyLocationConfigeration (config );
      173.  
      174. MLantitude = location. getLatitude ();
      175. MLongtitude = location. getlongpolling ();
      176.  
      177. LatLng currentLatLng = new LatLng (mLantitude, mLongtitude );
      178. MLoactionLatLng = new LatLng (mLantitude, mLongtitude );
      179.  
      180. // Whether to locate the task for the first time
      181. If (isFirstLoc ){
      182. IsFirstLoc = false;
      183. // Implement animation jump
      184. MapStatusUpdate u = MapStatusUpdateFactory
      185. . NewLatLng (currentLatLng );
      186. MBaiduMap. animateMapStatus (u );
      187.  
      188. MGeoCoder. reverseGeoCode (new ReverseGeoCodeOption ())
      189. . Location (currentLatLng ));
      190. Return;
      191. }
      192.  
      193. }
      194.  
      195. }
      196.  
      197. // Geographic code listener
      198. OnGetGeoCoderResultListener GeoListener = new OnGetGeoCoderResultListener (){
      199. Public void onGetGeoCodeResult (GeoCodeResult result ){
      200. If (result = null | result. error! = SearchResult. ERRORNO. NO_ERROR ){
      201. // No results retrieved
      202. }
      203. // Obtain the geocode result
      204. }
      205.  
      206. @ Override
      207. Public void onGetReverseGeoCodeResult (ReverseGeoCodeResult result ){
      208. If (result = null | result. error! = SearchResult. ERRORNO. NO_ERROR ){
      209. // No search results found
      210. }
      211. // Obtain the reverse geocode result
      212. Else {
      213. // Current location information
      214. MCurentInfo = new PoiInfo ();
      215. MCurentInfo. address = result. getAddress ();
      216. MCurentInfo. location = result. getLocation ();
      217. MCurentInfo. name = "[location]";
      218. MInfoList. clear ();
      219. MInfoList. add (mCurentInfo );
      220.  
      221. // Add the surrounding information to the table
      222. If (result. getPoiList ()! = Null ){
      223. MInfoList. addAll (result. getPoiList ());
      224. }
      225. // The Notification adaptation data has changed
      226. MAdapter. notifyDataSetChanged ();
      227. MLoadBar. setVisibility (View. GONE );
      228.  
      229. }
      230. }
      231. };
      232.  
      233. // Map touch event listener
      234. OnMapTouchListener touchListener = new OnMapTouchListener (){
      235. @ Override
      236. Public void onTouch (MotionEvent event ){
      237. // TODO Auto-generated method stub
      238. If (event. getAction () = MotionEvent. ACTION_UP ){
      239.  
      240. If (mCenterPoint = null ){
      241. Return;
      242. }
      243.  
      244. // Obtain the geographic coordinates corresponding to the screen coordinates of the current MapView Center
      245. LatLng currentLatLng;
      246. CurrentLatLng = mBaiduMap. getProjection (). fromScreenLocation (
      247. MCenterPoint );
      248. System. out. println ("----" + mCenterPoint. x );
      249. System. out. println ("----" + currentLatLng. latitude );
      250. // Initiate anti-geographic code search
      251. MGeoCoder. reverseGeoCode (new ReverseGeoCodeOption ())
      252. . Location (currentLatLng ));
      253. MLoadBar. setVisibility (View. VISIBLE );
      254.  
      255. }
      256. }
      257. };
      258.  
      259. // Click Event listener in the listView Option
      260. OnItemClickListener itemClickListener = new OnItemClickListener (){
      261.  
      262. @ Override
      263. Public void onItemClick (AdapterView Parent, View view, int position,
      264. Long id ){
      265. // TODO Auto-generated method stub
      266.  
      267. // The notification indicates that the position item of the adapter is selected.
      268. MAdapter. setpolicytip (position );
      269.  
      270. BitmapDescriptor mSelectIco = BitmapDescriptorFactory
      271. . FromResource (R. drawable. icon_geo );
      272. MBaiduMap. clear ();
      273. PoiInfo info = (PoiInfo) mAdapter. getItem (position );
      274. LatLng la = info. location;
      275.  
      276. // Animated jump
      277. MapStatusUpdate u = MapStatusUpdateFactory. newLatLng (la );
      278. MBaiduMap. animateMapStatus (u );
      279.  
      280. // Add a cover
      281. OverlayOptions ooA = new MarkerOptions (). position (la)
      282. . Icon (mSelectIco). anchor (0.5f, 0.5f );
      283. MBaiduMap. addOverlay (ooA );
      284.  
      285. // Check the selected items
      286. MSelectImg. setBackgroundResource (R. drawable. greywhite );
      287. MSelectImg = (ImageView) view. findViewById (R. id. place_select );
      288. MSelectImg. setBackgroundResource (R. drawable. ic_select );
      289.  
      290. // Uri mUri = Uri. parse ("geo: 39.940409, 116.355257 ");
      291. // Intent mIntent = new Intent (Intent. ACTION_VIEW, mUri );
      292. // StartActivity (mIntent );
      293.  
      294. }
      295.  
      296. };
      297. }

         

        Custom listView Adapter

         

        [Java]View plaincopy
        1. Package com. vr. souhuodong. UI. Adapter;
        2.  
        3. Import java. util. List;
        4.  
        5. Import android. R. integer;
        6. Import android. view. LayoutInflater;
        7. Import android. view. View;
        8. Import android. view. View. OnClickListener;
        9. Import android. view. ViewGroup;
        10. Import android. widget. BaseAdapter;
        11. Import android. widget. ImageView;
        12. Import android. widget. TextView;
        13.  
        14. Import com. baidu. mapapi. search. core. PoiInfo;
        15. Import com. vr. souhuodong. R;
        16.  
        17. Public class PlaceListAdapter extends BaseAdapter {
        18.  
        19. List MList;
        20. LayoutInflater mInflater;
        21. Int yytip;
        22.  
        23. Private class MyViewHolder {
        24. TextView placeName;
        25. TextView placeAddree;
        26. ImageView placeSelected;
        27. }
        28.  
        29. Public PlaceListAdapter (LayoutInflater mInflater, List MList ){
        30. Super ();
        31. This. mList = mList;
        32. This. mInflater = mInflater;
        33. Policytip =-1;
        34. }
        35.  
        36.  
        37. /**
        38. * Set the items to be selected
        39. * @ Param policytip
        40. */
        41. Public void setNotifyTip (int notifyTip ){
        42. This. policytip = policytip;
        43. }
        44.  
        45. @ Override
        46. Public int getCount (){
        47. // TODO Auto-generated method stub
        48. Return mList. size ();
        49. }
        50.  
        51. @ Override
        52. Public Object getItem (int position ){
        53. // TODO Auto-generated method stub
        54. Return mList. get (position );
        55. }
        56.  
        57. @ Override
        58. Public long getItemId (int position ){
        59. // TODO Auto-generated method stub
        60. Return position;
        61. }
        62.  
        63. @ Override
        64. Public View getView (int position, View convertView, ViewGroup parent ){
        65. // TODO Auto-generated method stub
        66. MyViewHolder holder;
        67. If (convertView = null ){
        68. System. out. println ("---- aa -");
        69. ConvertView = mInflater. inflate (com. vr. souhuodong. R. layout. listitem_place, parent, false );
        70. Holder = new MyViewHolder ();
        71. Holder. placeName = (TextView) convertView
        72. . FindViewById (com. vr. souhuodong. R. id. place_name );
        73. Holder. placeAddree = (TextView) convertView
        74. . FindViewById (com. vr. souhuodong. R. id. place_adress );
        75. Holder. placeSelected = (ImageView) convertView
        76. . FindViewById (com. vr. souhuodong. R. id. place_select );
        77. Holder. placeName. setText (mList. get (position). name );
        78. Holder. placeAddree. setText (mList. get (position). address );
        79. Holder. placeSelected. setBackgroundResource (R. drawable. greywhite );
        80. ConvertView. setTag (holder );
        81. } Else {
        82. Holder = (MyViewHolder) convertView. getTag ();
        83. }
        84. Holder. placeName. setText (mList. get (position). name );
        85. Holder. placeAddree. setText (mList. get (position). address );
        86. // Depending on whether the position item is selected during reload, select to load different images.
        87. If (policytip = position ){
        88. Holder. placeSelected. setBackgroundResource (R. drawable. ic_select );
        89. }
        90. Else {
        91. Holder. placeSelected. setBackgroundResource (R. drawable. greywhite );
        92. }
        93.  
        94. Return convertView;
        95. }
        96.  
        97.  
        98. // Class MyItemClickListener implements OnClickListener {
        99. //
        100. // ImageView mImg;
        101. // Public MyItemClickListener (ImageView mImg ){
        102. // This. mImg = mImg;
        103. //}
        104. // @ Override
        105. // Public void onClick (View v ){
        106. /// TODO Auto-generated method stub
        107. // MImg. setBackgroundResource (R. drawable. ic_select );
        108. //}
        109. //
        110. //}
        111.  
        112.  
        113. }

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.