Android Baidu Map SDK v3.0.0 (ii) positioning and combination direction sensor
Source: Internet
Author: User
<span id="Label3"></p><p><p>Reprint please indicate source: http://blog.csdn.net/lmj623565791/article/details/37730469</p></p><p><p>In the previous blog, we successfully imported the map into our Project. This article we are going to join for the map: first, positioning function; second. Combined with the direction Sensor. The direction of the road is confirmed by rotating the PHONE. With these two features, the map has been able to serve me ~ ~ ~</p></p><p><p>:</p></p><p style="text-align:center"><p style="text-align:center"><br></p></p><p style="text-align:left"><p style="text-align:left">well, to be able to code, for convenience, I put all the buttons in the MENU.</p></p>1. Initial start-up positioning<p style="text-align:left"><p style="text-align:left"></p></p><pre code_snippet_id="426361" snippet_file_name="blog_20140714_1_1262113" name="code" class="java"><pre code_snippet_id="426361" snippet_file_name="blog_20140714_1_1262113" name="code" class="java">/** * Targeted client */private locationclient mlocationclient;/** * Positioned listener */public mylocationlistener mmylocationlistener;/* * * Current positioning mode */private locationmode Mcurrentmode = locationmode.normal;/*** * is the first time to locate */private volatile Boolean Isfristlo cation = true;/** * Initialize location related code */private void initmylocation () {//positioning initialization mlocationclient = new Locationclient (this); mmylocat Ionlistener = new Mylocationlistener (); mlocationclient.registerlocationlistener (mmylocationlistener);// Set the relevant configuration for positioning locationclientoption option = new Locationclientoption (); Option.setopengps (true);// Open Gpsoption.setcoortype ("bd09ll"); Set coordinate type Option.setscanspan (+); mlocationclient.setlocoption (option);}</pre></pre><br>Then the locator listener Mylocationlistener:<br><pre code_snippet_id="426361" snippet_file_name="blog_20140714_2_5612748" name="code" class="java">/** * Implements a Real-bit callback listener */public class Mylocationlistener implements Bdlocationlistener{@Overridepublic void Onreceivelocation ( Bdlocation Location) {//map View is not processed after the newly received position is not handled if (site = = NULL | | mmapview = = Null) return;//construct location data mylocationdata LOCD ATA = new Mylocationdata.builder (). accuracy (location.getradius ())//this Sets the direction information that the developer obtains, clockwise 0-360.direction ( Mxdirection). latitude (location.getlatitude ()). longitude (location.getlongitude ()). build (); mcurrentaccracy = Location.getradius ();//set location data mbaidumap.setmylocationdata (locdata); mcurrentlantitude = Location.getlatitude (); Mcurrentlongitude = location.getlongitude ();//set own definition icon Bitmapdescriptor mcurrentmarker = Bitmapdescriptorfactory.fromresource (r.drawable.navi_map_gps_locked); mylocationconfigeration config = new mylocationconfigeration (mcurrentmode, true, mcurrentmarker); Mbaidumap.setmylocationconfigeration (config);//when positioning for the first time, move the map position to the current position if (isfristlocation) {isfristlocation = false; LATLNG ll = new LATLNG (location.getlatitude (), location.getlongitude ()); MApstatusupdate u = mapstatusupdatefactory.newlatlng (ll); mbaidumap.animatemapstatus (u);}} </pre>Can see that we initialized the position of the number of References. Set the positioning of the listener, every 1s will be positioned once, when the application opens, the first positioning, the map center will set the current user Location.<br>Positioning is also more power consumption, so we in the onstart to open the positioning in the onstop to close the positioning ~ ~ so that the application of the minimum will not always be where the GPS request location, users should see your app has been in that location, expected to be uninstalled immediately ~<p><p></p></p><p style="text-align:left"><p style="text-align:left"></p></p><pre code_snippet_id="426361" snippet_file_name="blog_20140714_3_8327963" name="code" class="java"><pre code_snippet_id="426361" snippet_file_name="blog_20140714_3_8327963" name="code" class="java">@Overrideprotected void OnStart () {//turn on layer positioning mbaidumap.setmylocationenabled (true); if (!mlocationclient.isstarted ()) { Mlocationclient.start ();} Open direction sensor Myorientationlistener.start (); Super.onstart ();} @Overrideprotected void onStop () {//turn off layer positioning mbaidumap.setmylocationenabled (false); mlocationclient.stop ();// Turn off direction sensor Myorientationlistener.stop (); Super.onstop ();}</pre></pre><br>The code of the sensor above will be introduced in a few ~<p><p></p></p><p style="text-align:left"><p style="text-align:left">Remember to androidmanifest.xml with a service</p></p><p style="text-align:left"><p style="text-align:left"></p></p><pre code_snippet_id="426361" snippet_file_name="blog_20140714_4_4074821" name="code" class="html"><pre code_snippet_id="426361" snippet_file_name="blog_20140714_4_4074821" name="code" class="html"> <service android:name= "com.baidu.location.f" android:enabled= "true" android:process= ": remote" > <intent-filter> <action android:name= "com.baidu.location.service_v2.2" > </ action> </intent-filter> </service></pre></pre><br>Now the main positioning function has been realized ~ just we need to add click-to-position button and direction sensor<p><p></p></p>2. My position<p style="text-align:left"><p style="text-align:left">Clicking on the My Location menu will call the Center2myloc Method.</p></p><p><p></p></p><p style="text-align:left"><p style="text-align:left"></p></p><pre code_snippet_id="426361" snippet_file_name="blog_20140714_5_3376710" name="code" class="java"><pre code_snippet_id="426361" snippet_file_name="blog_20140714_5_3376710" name="code" class="java">Case R.id.id_menu_map_myloc:center2myloc ();</pre></pre><br><pre code_snippet_id="426361" snippet_file_name="blog_20140714_6_5743261" name="code" class="java"><pre code_snippet_id="426361" snippet_file_name="blog_20140714_6_5743261" name="code" class="java">/** * Map moved to my location, where you can send the location request again, and then locate; * Directly take the recent latitude and longitude, assuming a long time without positioning success. May show a bad effect */private void Center2myloc () {latlng ll = new LATLNG (mcurrentlantitude, mcurrentlongitude); Mapstatusupdate u = mapstatusupdatefactory.newlatlng (ll); mbaidumap.animatemapstatus (u);}</pre></pre><br>Very easy. We have saved the most recent positioning latitude and longitude in the listener, so we just need to click and move the map to the corresponding Location.<p><p></p></p><p><p></p></p><p><p></p></p>3. Integrated Direction sensor<p style="text-align:left"><p style="text-align:left">The first is the class of the encapsulated directional sensor Myorientationlistener.java</p></p><p style="text-align:left"><p style="text-align:left"></p></p><pre code_snippet_id="426361" snippet_file_name="blog_20140714_7_4521762" name="code" class="java">Package Com.zhy.zhy_baidu_ditu_demo00;import Android.content.context;import Android.hardware.sensor;import Android.hardware.sensorevent;import Android.hardware.sensoreventlistener;import android.hardware.SensorManager; public class Myorientationlistener implements Sensoreventlistener{private Context context;private Sensormanager Sensormanager;private Sensor sensor;private Float lastx; Private Onorientationlistener onorientationlistener; Public Myorientationlistener (context Context) {this.context = context;} Start public void start () {//get sensor Manager Sensormanager = (sensormanager) Context.getsystemservice (context.sensor_service); If (sensormanager! = Null) {//get direction Sensors sensor = Sensormanager.getdefaultsensor (sensor.type_orientation);} Brochure If (sensor! = Null) {//sensormanager.sensor_delay_uisensormanager.registerlistener (this, sensor, sensormanager.sensor_delay_ui);}} Stop detecting public void stop () {sensormanager.unregisterlistener (this);} @Overridepublic void onaccuracychanged (sensor sensor, int Accuracy) {}@Overridepublic void onsensorchanged (sensorevent Event) {//accepts The type of direction sensor if (event.sensor.getType () = = Sensor.type_o Rientation) {//here We can get the Data. The float x = event.values[sensormanager.data_x] is then processed according to the need; If (math.abs (x-lastx) > 1.0) {onorientationlistener.onorientationchanged (x); }//LOG.E ("data_x", x+ ""); LASTX = x; }}public void Setonorientationlistener (onorientationlistener onorientationlistener) {this.onorientationlistener = onorientationlistener;} public interface Onorientationlistener {void onorientationchanged (float x);}}</pre><br>Initializing the direction sensor in the OnCreate<p><p></p></p><p style="text-align:left"><p style="text-align:left"></p></p><pre code_snippet_id="426361" snippet_file_name="blog_20140714_8_6124162" name="code" class="java"><pre code_snippet_id="426361" snippet_file_name="blog_20140714_8_6124162" name="code" class="java">/** * Initialize direction sensor */private void Initoritationlistener () {myorientationlistener = new Myorientationlistener ( Getapplicationcontext ()); myorientationlistener.setonorientationlistener (new onorientationlistener () {@ overridepublic void onorientationchanged (float x) {mxdirection = (int) x;//constructs positioning data mylocationdata locdata = new Mylocationd Ata. Builder (). Accuracy (mcurrentaccracy)//here Sets the direction information that the developer obtains, clockwise 0-360.direction (mxdirection). latitude ( mcurrentlantitude). Longitude (mcurrentlongitude). Build ();//set location data Mbaidumap.setmylocationdata (locdata);// Set your own definition icon bitmapdescriptor mcurrentmarker = Bitmapdescriptorfactory.fromresource (r.drawable.navi_map_gps_locked); mylocationconfigeration config = new mylocationconfigeration (mcurrentmode, true, mcurrentmarker); Mbaidumap.setmylocationconfigeration (config);}});}</pre></pre><br>finally, the directional sensors are switched on and off in OnStart and OnStop respectively.<p><p></p></p><p><p></p></p><p><p></p></p><p style="text-align:left"><p style="text-align:left">For rotating the phone to determine the direction, actually using the</p></p><p style="text-align:left"><p style="text-align:left"></p></p><pre code_snippet_id="426361" snippet_file_name="blog_20140714_9_4587019" name="code" class="java"><pre code_snippet_id="426361" snippet_file_name="blog_20140714_9_4587019" name="code" class="java">New Mylocationdata.builder ()//this Sets the direction information that the developer obtains, clockwise 0-360 . direction (mxdirection)</pre></pre>Just need to set the x-direction angle to be able to ~ ~ ~ is not very easy~~~<p><p></p></p><p style="text-align:left"><p style="text-align:left"><br></p></p><p style="text-align:left"><p style="text-align:left">well, the introduction has been completed, the switch to close the map style, as well as the trailing, compass and other modes of switching is not introduced, we look at the source code ~ ~</p></p><p style="text-align:left"><p style="text-align:left"><br></p></p><p style="text-align:left"><p style="text-align:left"><span style="font-size:24px; color:#006600">Source code click to download</span><br>Note: developer key needs to be replaced by its own application. If you do not know the application, please see the first blog.</p></p><p style="text-align:left"><p style="text-align:left"><br></p></p><p style="text-align:left"><p style="text-align:left"></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">Baidu Map Related Blog Video version number has been online: the use of Baidu map in Android look forward to your Support.</p></p><p><p></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><br></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">Bo Master part of the video has been online, assuming you do not like boring text. Please poke (first record). Looking forward to your support):</p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(51, 102, 153); font-size: 24px;">1. Android self-defined Control scratch card in real-action e-commerce activities</span></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 24px;">2.</span> <span style="font-size: 24px;">Android self-defining controls to build Android streaming layouts and popular tags</span><br></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 24px;">3.</span> <span style="font-size: 24px;">Android robot "little mu" implementation</span><br></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">4, High imitation QQ5.0 sideways slip<br></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(51, 102, 153); font-size: 24px;">5, High Imitation 5.2.1 main interface and message alert</span></p></p><br><br><p style="text-align:left"><p style="text-align:left"><br></p></p><p style="text-align:left"><p style="text-align:left"><br></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">Baidu Map Related Blog Video version number has been online: the use of Baidu map in Android look forward to your Support.</p></p><p><p></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><br></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><br></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">Bo Master part of the video has been online, if you do not like the boring text, please poke (first record). Looking forward to your support):</p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(51, 102, 153); font-size: 24px;">1. Android self-defined Control scratch card in real-action e-commerce activities</span></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 24px;">2.</span> <span style="font-size: 24px;">Android self-defining controls to build Android streaming layouts and popular tags</span><br></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 24px;">3.</span> <span style="font-size: 24px;">Android robot "little mu" implementation</span><br></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">4, High imitation QQ5.0 sideways slip<br></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(51, 102, 153); font-size: 24px;">5, High Imitation 5.2.1 main interface and message alert</span></p></p><br> <p><p>Android Baidu Map SDK v3.0.0 (ii) positioning and combination direction sensor</p></p></span>
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