Development of Baidu Map

Source: Internet
Author: User

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 ){
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;
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 = new geocoder (getbasecontext (), locale. getdefault ());
156. Try {
157. List <address> addresses = geocoder. getfromlocation (
158. Point. getlatitudee6 ()/1e6, point. getlongitudee6 ()/1e6,
159. 1 );
160. 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 ){
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"/>

Development of Baidu Map

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.