Development of Baidu map implementation and development of Baidu Map

Source: Internet
Author: User

Development of Baidu map implementation and development of Baidu Map

Marking the user's current location on a map is actually a GPS Positioning Application. First, obtain the longitude and latitude of the user's current location through GPS positioning, and then mark the point represented by this latitude and longitude on the map. In fact, in addition to marking our location on a map, we usually need to: "If my location changes, it must be reflected on the map in real time ".

Create the project baidumaplocation. design the main. xml file. Note that the MapView control must use com. baidu. mapapi. MapView encapsulated by the baidu Library. The design code is as follows:

Xml Code [url =]

 

 

 


[/Url]

1. <? Xml version = "1.0" encoding = "UTF-8"?>
2. <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
3. android: layout_width = "fill_parent"
4. android: layout_height = "fill_parent"
5. android: orientation = "vertical">
6. <FrameLayout
7. android: id = "@ + id/map_layout"
8. android: layout_width = "fill_parent"
9. android: layout_height = "fill_parent"
10. android: orientation = "vertical">
11. <! -- Baidu MapView control -->
12. <com. baidu. mapapi. MapView
13. android: id = "@ + id/map_view"
14. android: layout_width = "fill_parent"
15. android: layout_height = "fill_parent"
16. android: apiKey = "0Mg_koWoyZUiYLfZxmPfp4LKInB5LqTnagYueaw"
17. android: clickable = "true"
18. android: enabled = "true"/>
19. <LinearLayout
20. android: layout_width = "wrap_content"
21. android: layout_height = "wrap_content"
22. android: layout_gravity = "center"
23. android: orientation = "vertical"
24. android: paddingBottom = "105dip">
25. <! -- TextView for address information -->
26. <TextView
27. android: id = "@ + id/map_bubbleText"
28. android: layout_width = "wrap_content"
29. android: layout_height = "wrap_content"
30. android: background = "@ drawable/location_tips"
31. android: gravity = "left | center"
32. android: maxEms = "12"
33. android: paddingLeft = "12dip"
34. android: paddingRight = "10dip"
35. android: text = "@ string/load_tips"
36. android: textColor = "# cfcfcf"
37. android: textSize = "14sp"/>
38. </LinearLayout>
39. <LinearLayout
40. android: layout_width = "wrap_content"
41. android: layout_height = "wrap_content"
42. android: layout_gravity = "center"
43. android: orientation = "vertical">
44. <! -- Position indicator display ImageView -->
45. <ImageView
46. android: id = "@ + id/point_image"
47. android: layout_width = "wrap_content"
48. android: layout_height = "wrap_content"
49. android: layout_gravity = "center"
50. android: layout_marginBottom = "30dip"
51. android: src = "@ drawable/point_start"/>
52. </LinearLayout>
53. </FrameLayout>
54. </LinearLayout>

 


3. Create a MyMapOverlay that overwrites the entire map capture touch event to inherit Overlay.

Java code [url =]

 

 

 


[/Url]

1. import android. view. MotionEvent;
2. import com. baidu. mapapi. GeoPoint;
3. import com. baidu. mapapi. MapView;
4. import com. baidu. mapapi. Overlay;
5. // OverLay the whole map to capture touch events
6. public abstract class MyMapOverlay extends Overlay {
7. private int point_X;
8. private int point_Y;
9. private GeoPoint newPoint;
10. public MyMapOverlay (int x, int y ){
11. point_X = x;
12. point_Y = y;
13 .}
14. boolean flagMove = false;
15. // here, the coordinates of the longitude and latitude of the center of the screen are retrieved based on the movement of the map.
16. @ Override
17. public boolean onTouchEvent (MotionEvent event, MapView mapView ){
18. System. out. println ("X->" + event. getX () + ":" + point_X );
19. System. out. println ("Y->" + event. getY () + ":" + point_Y );
20. if (event. getAction () = MotionEvent. ACTION_DOWN ){
21. changePoint (newPoint, 1 );
22.} else if (event. getAction () = MotionEvent. ACTION_UP ){
23. newPoint = mapView. getProjection (). fromPixels (point_X, point_Y );
24. changePoint (newPoint, 2 );
25 .}
26. return false;
27 .}
28.
29. public abstract void changePoint (GeoPoint newPoint, int type );
30 .}

 


4. The LocationActivity class inherits the MapActivity of Baidu database and implements the LocationListener interface. The Code is as follows:
Package com. location. activity; Java code [url =]

 

 

 


[/Url]

1. import java. io. IOException;
2. import java. util. List;
3. import java. util. Locale;
4.
5. import android. content. Intent;
6. import android. location. Address;
7. import android. location. Geocoder;
8. import android. location. Location;
9. import android. OS. Bundle;
10. import android. OS. Handler;
11. import android. OS. Message;
12. import android. view. View;
13. import android. view. Window;
14. import android. widget. TextView;
15.
16. import com. android. map. MyMapOverlay;
17. import com. baidu. mapapi. BMapManager;
18. import com. baidu. mapapi. GeoPoint;
19. import com. baidu. mapapi. LocationListener;
20. import com. baidu. mapapi. MKAddrInfo;
21. import com. baidu. mapapi. MKBusLineResult;
22. import com. baidu. mapapi. MKDrivingRouteResult;
23. import com. baidu. mapapi. MKLocationManager;
24. import com. baidu. mapapi. MKPoiResult;
25. import com. baidu. mapapi. MKSearch;
26. import com. baidu. mapapi. MKSearchListener;
27. import com. baidu. mapapi. MKSuggestionResult;
28. import com. baidu. mapapi. MKTransitRouteResult;
29. import com. baidu. mapapi. MKWalkingRouteResult;
30. import com. baidu. mapapi. MapActivity;
31. import com. baidu. mapapi. MapController;
32. import com. baidu. mapapi. MapView;
33. import com. baidu. mapapi. Overlay;
34.
35. public class LocationActivity extends MapActivity implements LocationListener {
36.
37. private MapView mapView;
38. private MapController mMapCtrl;
39. private List <Overlay> mapOverlays;
40. public GeoPoint locPoint;
41. private MyMapOverlay mOverlay;
42. private TextView desText;
43. private String lost_tips;
44. private int point_X;
45. private int point_Y;
46.
47. public final int MSG_VIEW_LONGPRESS = 10001;
48. public final int MSG_VIEW_ADDRESSNAME = 10002;
49. public final int MSG_GONE_ADDRESSNAME = 10003;
50. private Intent mIntent;
51. private int mLatitude;
52. private int mlongpolling;
53. private String name;
54. private BMapManager mapManager;
55. private MKLocationManager mLocationManager = null;
56. private boolean isLoadAdrr = true;
57. private MKSearch mMKSearch;
58.
59. @ Override
60. public void onCreate (Bundle savedInstanceState ){
61. super. onCreate (savedInstanceState );
62. requestWindowFeature (Window. FEATURE_NO_TITLE );
63. setContentView (R. layout. main );
64. initMap ();
65. mIntent = getIntent ();
66. mlattra = mIntent. getIntExtra ("latitude", 0 );
67. mlongpolling = mIntent. getIntExtra ("longpolling", 0 );
68. name = mIntent. getStringExtra ("name ");
69. mapView = (MapView) findViewById (R. id. map_view );
70. desText = (TextView) this. findViewById (R. id. map_bubbleText );
71. lost_tips = getResources (). getString (R. string. load_tips );
72. if (mLatitude! = 0 & mlongpolling! = 0 ){
73. locPoint = new GeoPoint (int) (mLatitude * 1E6 ),
74. (int) (mlong1_* 1E6 ));
75. desText. setText (name );
76 .}
77. mapView. setBuiltInZoomControls (true );
78. mapView. setClickable (true );
79. mMapCtrl = mapView. getController ();
80. point_X = this. getWindowManager (). getdefadisplay display (). getWidth ()/2;
81. point_Y = this. getWindowManager (). getdefadisplay display (). getHeight ()/2;
82. mOverlay = new MyMapOverlay (point_X, point_Y ){
83. @ Override
84. public void changePoint (GeoPoint newPoint, int type ){
85. if (type = 1 ){
86. mHandler. sendEmptyMessage (MSG_GONE_ADDRESSNAME );
87.} else {
88. locPoint = newPoint;
89. mHandler. sendEmptyMessage (MSG_VIEW_LONGPRESS );
90 .}
91.
92 .}
93 .};
94. mapOverlays = mapView. getOverlays ();
95. if (mapOverlays. size ()> 0 ){
96. mapOverlays. clear ();
97 .}
98. mapOverlays. add (mOverlay );
99. mMapCtrl. setZoom (20 );
100.
101 .}
102.
103. private void initMap (){
104.
105. // initialize MapActivity
106. mapManager = new BMapManager (getApplication ());
107. // The first parameter of the init method must be filled with the applied API Key
108. mapManager. init ("C66C0501D0280744759A6957C42543AE38F5D540", null );
109. super. initMapActivity (mapManager );
110. // instantiate the search address class
111. mMKSearch = new MKSearch ();
112. // initialize the search address instance
113. mMKSearch. init (mapManager, new MySearchListener ());
114. mLocationManager = mapManager. getLocationManager ();
115. // register a location update event
116. mLocationManager. requestLocationUpdates (this );
117. // use GPS to locate
118. mLocationManager
119 .. enableProvider (int) MKLocationManager. MK_GPS_PROVIDER );
120 .}
121.
122. @ Override
123. protected void onResume (){
124. if (mapManager! = Null ){
125. mapManager. start ();
126 .}
127. super. onResume ();
128.
129 .}
130.
131. @ Override
132. protected void onPause (){
133. isLoadAdrr = false;
134. if (mapManager! = Null ){
135. mapManager. stop ();
136 .}
137. super. onPause ();
138 .}
139.
140. @ Override
141. protected boolean isRouteDisplayed (){
142. // TODO Auto-generated method stub
143. return false;
144 .}
145.
146.
147 ./**
148. * obtain the address through latitude and longitude
149 .*
150. * @ param point
151. * @ return
152 .*/
153. private String getLocationAddress (GeoPoint point ){
154. String add = "";
155. Geocoder geoCoder = new Geocoder (getBaseContext (), Locale. getDefault ());
156. try {
157. List <Address> addresses = geoCoder. getFromLocation (
158. point. getLatitudeE6 ()/1E6, point. getLongitudeE6 ()/1E6,
159. 1 );
160. Address address = addresses. get (0 );
161. int maxLine = address. getMaxAddressLineIndex ();
162. if (maxLine> = 2 ){
163. add = address. getAddressLine (1) + address. getAddressLine (2 );
164.} else {
165. add = address. getAddressLine (1 );
166 .}
167.} catch (IOException e ){
168. add = "";
169. e. printStackTrace ();
170 .}
171. return add;
172 .}
173.
174.
175. private Handler mHandler = new Handler (){
176. @ Override
177. public void handleMessage (Message msg ){
178. switch (msg. what ){
179. case MSG_VIEW_LONGPRESS: // process the location information returned by a long time
180 .{
181. if (null = locPoint)
182. return;
183. mMKSearch. reverseGeocode (locPoint );
184. desText. setVisibility (View. VISIBLE );
185. desText. setText (lost_tips );
186. mMapCtrl. animateTo (locPoint );
187. mapView. invalidate ();
188 .}
189. break;
190. case MSG_VIEW_ADDRESSNAME:
191. desText. setText (String) msg. obj );
192. desText. setVisibility (View. VISIBLE );
193. break;
194. case MSG_GONE_ADDRESSNAME:
195. desText. setVisibility (View. GONE );
196. break;
197 .}
198 .}
199 .};
200.
201. // close the program and disable positioning.
202. @ Override
203. protected void onDestroy (){
204. if (mapManager! = Null ){
205. mapManager. destroy ();
206. mapManager = null;
207 .}
208. super. onDestroy ();
209 .}
210.
211 ./**
212. * determine whether to display the current location on the map based on the attributes configured by MyLocationOverlay.
213 .*/
214. @ Override
215. protected boolean isLocationDisplayed (){
216. return false;
217 .}
218.
219 ./**
220. * This method is triggered when the position changes
221 .*
222. * @ param location
223. * Current location
224 .*/
225. public void onLocationChanged (Location location ){
226. if (location! = Null ){
227. locPoint = new GeoPoint (int) (location. getLatitude () * 1E6 ),
228. (int) (location. getlongpolling () * 1E6 ));
229. mHandler. sendEmptyMessage (MSG_VIEW_LONGPRESS );
230 .}
231 .}
232.
233 ./**
234. * The internal class implements the MKSearchListener interface to Implement Asynchronous search services
235 .*
236. * @ author liufeng
237 .*/
238. public class MySearchListener implements MKSearchListener {
239 ./**
240. * search the address information result based on the latitude and longitude
241 .*
242. * @ param result
243. * search results
244. * @ param iError
245. * error no. (0 indicates correct return)
246 .*/
247. public void onGetAddrResult (MKAddrInfo result, int iError ){
248. if (result = null ){
249. return;
250 .}
251. Message msg = new Message ();
252. msg. what = MSG_VIEW_ADDRESSNAME;
253. msg. obj = result. strAddr;
254. mHandler. sendMessage (msg );
255.
256 .}
257.
258 ./**
259. * driving route search results
260 .*
261. * @ param result
262. * search results
263. * @ param iError
264. * error no. (0 indicates correct return)
265 .*/
266. public void onGetDrivingRouteResult (MKDrivingRouteResult result,
267. int iError ){
268 .}
269.
270 ./**
271. * POI search results (range search, city POI search, and peripheral search)
272 .*
273. * @ param result
274. * search results
275. * @ param type
276. * return result type (, 21: poi list 7: City list)
277. * @ param iError
278. * error no. (0 indicates correct return)
279 .*/
280. public void onGetPoiResult (MKPoiResult result, int type, int iError ){
281 .}
282.
283 ./**
284. * search results of bus transfer routes
285 .*
286. * @ param result
287. * search results
288. * @ param iError
289. * error no. (0 indicates correct return)
290 .*/
291. public void onGetTransitRouteResult (MKTransitRouteResult result,
292. int iError ){
293 .}
294.
295 ./**
296. * walking Route Search Results
297 .*
298. * @ param result
299. * search results
300. * @ param iError
301. * error no. (0 indicates correct return)
302 .*/
303. public void onGetWalkingRouteResult (MKWalkingRouteResult result,
304. int iError ){
305 .}
306.
307. public void onGetBusDetailResult (MKBusLineResult arg0, int arg1 ){
308. // TODO Auto-generated method stub
309.
310 .}
311.
312. public void onGetSuggestionResult (MKSuggestionResult arg0, int arg1 ){
313. // TODO Auto-generated method stub
314.
315 .}
316 .}
317.
318 .}

 


5. Add related access permissions in AndroidManifest. xml

<! -- Access Network permissions -->
Xml Code [url =]

 

 

 


[/Url]

1. <uses-permission android: name = "android. permission. INTERNET"/>
2. <! -- Permission for precise access -->
3. <uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"/>
4. <! -- Access network status permissions -->
5. <uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"/>
6. <! -- Access the WIFI network status -->
7. <uses-permission android: name = "android. permission. ACCESS_WIFI_STATE"/>
8. <! -- Change the permission of the Wi-Fi network -->
9. <uses-permission android: name = "android. permission. CHANGE_WIFI_STATE"/>
10. <! -- Read and write permissions on the memory card -->
11. <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
12. <! -- Permission to read the phone status -->
13. <uses-permission android: name = "android. permission. READ_PHONE_STATE"/>


Baidu map was developed by others

Speaking of the implementation of MapBar, today I sent a MapBar JS, which can be obtained from Baidu map (map.baidu.com/). Through this JS, I realized what I call the map display part, map container, control Panel (zoom in, zoom out, zoom out status bar, move left, move right, move up, move down, measure distance), scale, LOGO, bubble box (the shadow box in GOOGLE ), prompt information box (click or hover your mouse over a square prompt box on the point of interest), draw points, draw lines, and so on.
The show.htm In the compressed package can now display the map. For the effect, see the following figure. However, many images are obtained from the MapBar map image server, some images such as bubble boxes are also on the image server. In the past, I wanted to download maps of various scales on the MapBar image server to a local location, so that I could display the map locally, completely away from the MapBar image server, I have finished the non-map part, but I have not completed the map part because of the algorithm, in addition, because the image volume is too large (it is suspected that the image files on the image server of the MapBar account for dozens of GB), only the following parts are available. However, based on this principle, we can achieve the same effect by dividing our map by width (this algorithm should also be available in JS ).
The most important file in the compressed package is JS/avinclude. js. I have processed this JS in a simple branch, which is easier to read than the original line break. I basically didn't make any changes to the webpage and JS sections, but changed the simplest part without affecting the global.
What I don't understand is: the scale in the lower left corner of the MapBar. I think the problem is very big. The difference between the scale and the 51DITU is too large, I don't know if it is a 51DITU error or a MapBar error. What's important is how the scale is determined? (Maybe I have not read JS yet, but I should have understood it.) What is the map projection used by their maps? (The map projection part is even less clear. Put it to the smallest and largest. A map of the world and a city detail should be two completely different projections.) Maybe I have considered too much, maybe they simply mean that the difference between the projection scale and so on is not so careful.
If you like it, you can download the research. I hope you will have more experiences after the research. You are welcome to join us.

Android development: how to implement the following Baidu map functions on android: search results are obtained based on keywords entered by users

Is this the development of Baidu map on android?
Let's take a look at the example of Baidu map android api.

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.