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
- 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
- CurrentLatLng = mBaiduMap. getProjection (). fromScreenLocation (
- 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
- Package com. vr. souhuodong. UI. Sou;
-
- Import java. util. ArrayList;
- Import java. util. List;
-
- Import android. app. Activity;
- Import android. content. Intent;
- Import android. graphics. Point;
- Import android.net. Uri;
- Import android. OS. Bundle;
- Import android. view. MotionEvent;
- Import android. view. View;
- Import android. widget. AdapterView;
- Import android. widget. AdapterView. OnItemClickListener;
- Import android. widget. ImageView;
- Import android. widget. ListView;
- Import android. widget. ProgressBar;
-
- Import com. baidu. location. BDLocation;
- Import com. baidu. location. BDLocationListener;
- Import com. baidu. location. LocationClient;
- Import com. baidu. location. LocationClientOption;
- Import com. baidu. mapapi. map. BaiduMap;
- Import com. baidu. mapapi. map. BaiduMap. OnMapTouchListener;
- Import com. baidu. mapapi. map. BitmapDescriptor;
- Import com. baidu. mapapi. map. BitmapDescriptorFactory;
- Import com. baidu. mapapi. map. MapStatusUpdate;
- Import com. baidu. mapapi. map. MapStatusUpdateFactory;
- Import com. baidu. mapapi. map. MapView;
- Import com. baidu. mapapi. map. MarkerOptions;
- Import com. baidu. mapapi. map. MyLocationConfiguration;
- Import com. baidu. mapapi. map. MyLocationConfiguration. LocationMode;
- Import com. baidu. mapapi. map. MyLocationData;
- Import com. baidu. mapapi. map. OverlayOptions;
- Import com. baidu. mapapi. model. LatLng;
- Import com. baidu. mapapi. search. core. PoiInfo;
- Import com. baidu. mapapi. search. core. SearchResult;
- Import com. baidu. mapapi. search. geocode. GeoCodeResult;
- Import com. baidu. mapapi. search. geocode. GeoCoder;
- Import com. baidu. mapapi. search. geocode. OnGetGeoCoderResultListener;
- Import com. baidu. mapapi. search. geocode. ReverseGeoCodeOption;
- Import com. baidu. mapapi. search. geocode. ReverseGeoCodeResult;
- Import com. vr. souhuodong. R;
- Import com. vr. souhuodong. UI. Adapter. PlaceListAdapter;
-
- Public class ChoosePlaceActivity extends Activity {
-
- MapView mMapView;
- BaiduMap mBaiduMap;
- ProgressBar mLoadBar;
- ImageView mSelectImg;
-
- // Locate
- LocationClient mLocationClient = null;
- MyBDLocationListner mListner = null;
- BitmapDescriptor mCurrentMarker = null;
-
- // Current longitude and latitude
- Double mLantitude;
- Double mLongtitude;
- LatLng mLoactionLatLng;
-
- // Set the marker for the first time
- Boolean isFirstLoc = true;
-
- // Screen coordinates of the MapView Center
- Point mCenterPoint = null;
-
- // Geocode
- GeoCoder mGeoCoder = null;
-
- // Location list
- ListView mListView;
- PlaceListAdapter mAdapter;
- List MInfoList;
- PoiInfo mCurentInfo;
-
- @ Override
- Protected void onCreate (Bundle savedInstanceState ){
- // TODO Auto-generated method stub
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. activity_chooseplace );
-
- InitView ();
- }
-
- /**
- * Initialization Interface
- */
- Private void initView (){
- // TODO Auto-generated method stub
- // Initialize the map
- MMapView = (MapView) findViewById (R. id. chooseplace_bmapView );
- MMapView. showZoomControls (false );
- MBaiduMap = mMapView. getMap ();
- MapStatusUpdate msu = MapStatusUpdateFactory. zoomTo (17.0f );
- MBaiduMap. setMapStatus (msu );
- MBaiduMap. setOnMapTouchListener (touchListener );
-
- // Initialize the POI Information List
- MInfoList = new ArrayList ();
-
- // Initialize the screen coordinates of the current MapView center and the current geographic coordinates
- MCenterPoint = mBaiduMap.getMapStatus().tar getScreen;
- MLoactionLatLng = mBaiduMap.getMapStatus().tar get;
-
- // Locate
- MBaiduMap. setMyLocationEnabled (true );
- MLocationClient = new LocationClient (this );
- MListner = new MyBDLocationListner ();
- MLocationClient. registerLocationListener (mListner );
- LocationClientOption option = new LocationClientOption ();
- Option. setOpenGps (true); // enable gps
- Option. setCoorType ("bd09ll"); // sets the coordinate type.
- Option. setScanSpan (1000 );
- MLocationClient. setLocOption (option );
- MLocationClient. start ();
-
- // Geocode
- MGeoCoder = GeoCoder. newInstance ();
- MGeoCoder. setOnGetGeoCodeResultListener (GeoListener );
-
- // List of surrounding locations
- MListView = (ListView) findViewById (R. id. place_list );
- MLoadBar = (ProgressBar) findViewById (R. id. place_progressBar );
- MListView. setOnItemClickListener (itemClickListener );
- MAdapter = new PlaceListAdapter (getLayoutInflater (), mInfoList );
- MListView. setAdapter (mAdapter );
-
- MSelectImg = new ImageView (this );
- }
-
- Public void turnBack (View view ){
- // Implement animation jump
- MapStatusUpdate u = MapStatusUpdateFactory. newLatLng (mLoactionLatLng );
- MBaiduMap. animateMapStatus (u );
-
- MBaiduMap. clear ();
- // Initiate anti-geographic code search
- MGeoCoder. reverseGeoCode (new ReverseGeoCodeOption ())
- . Location (mLoactionLatLng ));
-
- }
-
- @ Override
- Protected void onDestroy (){
- // TODO Auto-generated method stub
- Super. onDestroy ();
- MLocationClient. stop ();
- MGeoCoder. destroy ();
- }
-
- // Locate the listener
- Private class MyBDLocationListner implements BDLocationListener {
-
- @ Override
- Public void onReceiveLocation (BDLocation location ){
- // TODO Auto-generated method stub
- // After map view is destroyed, it is not processed in the new receiving location
- If (location = null | mMapView = null)
- Return;
- MyLocationData data = new MyLocationData. Builder ()//
- //. Direction (mCurrentX )//
- . Accuracy (location. getRadius ())//
- . Latitude (location. getLatitude ())//
- . Longpolling (location. getlongpolling ())//
- . Build ();
- MBaiduMap. setMyLocationData (data );
- // Set the custom icon
- MyLocationConfiguration config = new MyLocationConfiguration (
- LocationMode. NORMAL, true, null );
- MBaiduMap. setMyLocationConfigeration (config );
-
- MLantitude = location. getLatitude ();
- MLongtitude = location. getlongpolling ();
-
- LatLng currentLatLng = new LatLng (mLantitude, mLongtitude );
- MLoactionLatLng = new LatLng (mLantitude, mLongtitude );
-
- // Whether to locate the task for the first time
- If (isFirstLoc ){
- IsFirstLoc = false;
- // Implement animation jump
- MapStatusUpdate u = MapStatusUpdateFactory
- . NewLatLng (currentLatLng );
- MBaiduMap. animateMapStatus (u );
-
- MGeoCoder. reverseGeoCode (new ReverseGeoCodeOption ())
- . Location (currentLatLng ));
- Return;
- }
-
- }
-
- }
-
- // Geographic code listener
- OnGetGeoCoderResultListener GeoListener = new OnGetGeoCoderResultListener (){
- Public void onGetGeoCodeResult (GeoCodeResult result ){
- If (result = null | result. error! = SearchResult. ERRORNO. NO_ERROR ){
- // No results retrieved
- }
- // Obtain the geocode result
- }
-
- @ Override
- Public void onGetReverseGeoCodeResult (ReverseGeoCodeResult result ){
- If (result = null | result. error! = SearchResult. ERRORNO. NO_ERROR ){
- // No search results found
- }
- // Obtain the reverse geocode result
- Else {
- // Current location information
- MCurentInfo = new PoiInfo ();
- MCurentInfo. address = result. getAddress ();
- MCurentInfo. location = result. getLocation ();
- MCurentInfo. name = "[location]";
- MInfoList. clear ();
- MInfoList. add (mCurentInfo );
-
- // Add the surrounding information to the table
- If (result. getPoiList ()! = Null ){
- MInfoList. addAll (result. getPoiList ());
- }
- // The Notification adaptation data has changed
- MAdapter. notifyDataSetChanged ();
- MLoadBar. setVisibility (View. GONE );
-
- }
- }
- };
-
- // Map touch event listener
- OnMapTouchListener touchListener = new OnMapTouchListener (){
- @ Override
- Public void onTouch (MotionEvent event ){
- // TODO Auto-generated method stub
- If (event. getAction () = MotionEvent. ACTION_UP ){
-
- If (mCenterPoint = null ){
- Return;
- }
-
- // Obtain the geographic coordinates corresponding to the screen coordinates of the current MapView Center
- LatLng currentLatLng;
- CurrentLatLng = mBaiduMap. getProjection (). fromScreenLocation (
- MCenterPoint );
- System. out. println ("----" + mCenterPoint. x );
- System. out. println ("----" + currentLatLng. latitude );
- // Initiate anti-geographic code search
- MGeoCoder. reverseGeoCode (new ReverseGeoCodeOption ())
- . Location (currentLatLng ));
- MLoadBar. setVisibility (View. VISIBLE );
-
- }
- }
- };
-
- // Click Event listener in the listView Option
- OnItemClickListener itemClickListener = new OnItemClickListener (){
-
- @ Override
- Public void onItemClick (AdapterView Parent, View view, int position,
- Long id ){
- // TODO Auto-generated method stub
-
- // The notification indicates that the position item of the adapter is selected.
- MAdapter. setpolicytip (position );
-
- BitmapDescriptor mSelectIco = BitmapDescriptorFactory
- . FromResource (R. drawable. icon_geo );
- MBaiduMap. clear ();
- PoiInfo info = (PoiInfo) mAdapter. getItem (position );
- LatLng la = info. location;
-
- // Animated jump
- MapStatusUpdate u = MapStatusUpdateFactory. newLatLng (la );
- MBaiduMap. animateMapStatus (u );
-
- // Add a cover
- OverlayOptions ooA = new MarkerOptions (). position (la)
- . Icon (mSelectIco). anchor (0.5f, 0.5f );
- MBaiduMap. addOverlay (ooA );
-
- // Check the selected items
- MSelectImg. setBackgroundResource (R. drawable. greywhite );
- MSelectImg = (ImageView) view. findViewById (R. id. place_select );
- MSelectImg. setBackgroundResource (R. drawable. ic_select );
-
- // Uri mUri = Uri. parse ("geo: 39.940409, 116.355257 ");
- // Intent mIntent = new Intent (Intent. ACTION_VIEW, mUri );
- // StartActivity (mIntent );
-
- }
-
- };
- }
Custom listView Adapter
[Java]View plaincopy
- Package com. vr. souhuodong. UI. Adapter;
-
- Import java. util. List;
-
- Import android. R. integer;
- Import android. view. LayoutInflater;
- Import android. view. View;
- Import android. view. View. OnClickListener;
- Import android. view. ViewGroup;
- Import android. widget. BaseAdapter;
- Import android. widget. ImageView;
- Import android. widget. TextView;
-
- Import com. baidu. mapapi. search. core. PoiInfo;
- Import com. vr. souhuodong. R;
-
- Public class PlaceListAdapter extends BaseAdapter {
-
- List MList;
- LayoutInflater mInflater;
- Int yytip;
-
- Private class MyViewHolder {
- TextView placeName;
- TextView placeAddree;
- ImageView placeSelected;
- }
-
- Public PlaceListAdapter (LayoutInflater mInflater, List MList ){
- Super ();
- This. mList = mList;
- This. mInflater = mInflater;
- Policytip =-1;
- }
-
-
- /**
- * Set the items to be selected
- * @ Param policytip
- */
- Public void setNotifyTip (int notifyTip ){
- This. policytip = policytip;
- }
-
- @ Override
- Public int getCount (){
- // TODO Auto-generated method stub
- Return mList. size ();
- }
-
- @ Override
- Public Object getItem (int position ){
- // TODO Auto-generated method stub
- Return mList. get (position );
- }
-
- @ Override
- Public long getItemId (int position ){
- // TODO Auto-generated method stub
- Return position;
- }
-
- @ Override
- Public View getView (int position, View convertView, ViewGroup parent ){
- // TODO Auto-generated method stub
- MyViewHolder holder;
- If (convertView = null ){
- System. out. println ("---- aa -");
- ConvertView = mInflater. inflate (com. vr. souhuodong. R. layout. listitem_place, parent, false );
- Holder = new MyViewHolder ();
- Holder. placeName = (TextView) convertView
- . FindViewById (com. vr. souhuodong. R. id. place_name );
- Holder. placeAddree = (TextView) convertView
- . FindViewById (com. vr. souhuodong. R. id. place_adress );
- Holder. placeSelected = (ImageView) convertView
- . FindViewById (com. vr. souhuodong. R. id. place_select );
- Holder. placeName. setText (mList. get (position). name );
- Holder. placeAddree. setText (mList. get (position). address );
- Holder. placeSelected. setBackgroundResource (R. drawable. greywhite );
- ConvertView. setTag (holder );
- } Else {
- Holder = (MyViewHolder) convertView. getTag ();
- }
- Holder. placeName. setText (mList. get (position). name );
- Holder. placeAddree. setText (mList. get (position). address );
- // Depending on whether the position item is selected during reload, select to load different images.
- If (policytip = position ){
- Holder. placeSelected. setBackgroundResource (R. drawable. ic_select );
- }
- Else {
- Holder. placeSelected. setBackgroundResource (R. drawable. greywhite );
- }
-
- Return convertView;
- }
-
-
- // Class MyItemClickListener implements OnClickListener {
- //
- // ImageView mImg;
- // Public MyItemClickListener (ImageView mImg ){
- // This. mImg = mImg;
- //}
- // @ Override
- // Public void onClick (View v ){
- /// TODO Auto-generated method stub
- // MImg. setBackgroundResource (R. drawable. ic_select );
- //}
- //
- //}
-
-
- }