Baidu map location sign-in function, Baidu map sign-in

Source: Internet
Author: User

Baidu map location sign-in function, Baidu map sign-in

1. Note that the key must be before the activity.

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
</service>

<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="6dsXhPU5WWHcBxGwQtHZKIGfZZBorMZ5" />
2. xml file
<Com. baidu. mapapi. map. MapView
Android: id = "@ + id/map_view"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">

</Com. baidu. mapapi. map. MapView>

3. java File
Public class LoactionView extends AppCompatActivity {
MapView mMapView;
BaiduMap mBaiduMap;
LocationClient mLocationClient;
LocationClientOption mOption;
Boolean isFirstLoc = true; // whether to locate the object for the first time
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SDKInitializer. initialize (getApplicationContext ());
SetContentView (R. layout. activity_loaction_view );
MMapView = (MapView) findViewById (R. id. map_view );
MBaiduMap = mMapView. getMap ();
MBaiduMap. setMyLocationEnabled (true );
MLocationClient = new LocationClient (getApplicationContext ());
MLocationClient. setLocOption (getdefaloclocationclientoption ());
MLocationClient. registerLocationListener (mBDLocationListener );
MLocationClient. start ();

MMapView. refreshDrawableState ();
}
/***
*
* @ Return defaloclocationclientoption
*/
Public LocationClientOption getdefaloclocationclientoption (){

MOption = new LocationClientOption ();
MOption. setLocationMode (LocationClientOption. LocationMode. Hight_Accuracy); // optional. The default value is high precision. It sets the positioning mode with high precision and low power consumption, and only applies to devices.
MOption. setCoorType ("bd09ll"); // optional, gcj02 by default. It sets the returned Positioning Result coordinate system. If it is used with Baidu map, it is recommended to set it to bd09ll;
// MOption. setScanSpan (0); // optional. The default value is 0, indicating that the request is located only once. setting that the interval between request initiation and location must be greater than or equal to MS is valid.
MOption. setIsNeedAddress (true); // optional. It specifies whether address information is required. This parameter is not required by default.
MOption. setIsNeedLocationDescribe (true); // optional, whether the address description is required
MOption. setNeedDeviceDirect (false); // optional, set whether the device direction result is required
MOption. setLocationNotify (false); // optional. The default value is false. It determines whether to output the gps result at 1 S1 frequency when the GPS is valid.
MOption. setIgnoreKillProcess (true); // optional. The default value is true. It is used to locate the SDK as a SERVICE and put it into an independent process. It sets whether to kill the process during the stop process. By default, the process is not killed.
MOption. setIsNeedLocationDescribe (true); // optional. The default value is false. You can set whether location semantics is required. The result can be obtained in BDLocation. getLocationDescribe. The result is similar to "near Tiananmen Square, Beijing"
MOption. setIsNeedLocationPoiList (true); // optional. The default value is false. Set whether POI results are required. You can obtain the result in BDLocation. getPoiList.
MOption. SetIgnoreCacheException (false); // optional. The default value is false. It indicates whether to collect CRASH information. The default value is false.

Return mOption;
}
Private BDLocationListener mBDLocationListener = new BDLocationListener (){

@ Override
Public void onReceiveLocation (BDLocation location ){
If (location = null | mMapView = null)
Return;
MyLocationData locData = new MyLocationData. Builder ().
Accuracy (location. getRadius (). direction (100). latitude (location. getlongdistance (). build ();
MBaiduMap. setMyLocationData (locData );
If (isFirstLoc ){
IsFirstLoc = false;
LatLng ll = new LatLng (location. getLatitude (),
Location. getlongpolling ());

// Map Annotation
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory. fromResource (R. drawable. icon_geo );
OverlayOptions options = new MarkerOptions (). position (ll). icon (bitmapDescriptor );
MBaiduMap. addOverlay (options );
/*
* Circle
**/
CircleOptions circleOptions = new CircleOptions ();
CircleOptions. center (ll); // you can specify the coordinates of the center.
CircleOptions. fillColor (0Xaafaa355); // fill color of the circle
CircleOptions. fillColor (0Xaafaa355); // fill color of the circle
CircleOptions. radius (400); // set the radius
CircleOptions. stroke (new Stroke (2, 0xAA00FF00); // you can specify a border.
MBaiduMap. addOverlay (circleOptions );

MapStatusUpdate u = MapStatusUpdateFactory. newLatLngZoom (ll, 16); // you can specify the map center and zoom level.
// MapStatusUpdate u = MapStatusUpdateFactory. newLatLng (ll );
MBaiduMap. animateMapStatus (u );
}


// TODO Auto-generated method stub
If (null! = Location & location. getLocType ()! = BDLocation. TypeServerError ){
StringBuffer sb = new StringBuffer (256 );
Sb. append ("time :");
/**
* You can also use the systemClock. elapsedRealtime () method to obtain the time of each callback since the startup;
* Location. getTime () indicates the time when the service end outputs the result. If the location does not change, the time remains unchanged.
*/
Sb. append (location. getTime ());
Sb. append ("\ nerror code :");
Sb. append (location. getLocType ());
Sb. append ("\ nlatitude :");
Sb. append (location. getLatitude ());
Sb. append ("\ nlontitude :");
Sb. append (location. getlongpolling ());
Sb. append ("\ nradius :");
Sb. append (location. getRadius ());
Sb. append ("\ nCountryCode :");
Sb. append (location. getCountryCode ());
Sb. append ("\ nCountry :");
Sb. append (location. getCountry ());
Sb. append ("\ ncitycode :");
Sb. append (location. getCityCode ());
Sb. append ("\ ncity :");
Sb. append (location. getCity ());
Sb. append ("\ nDistrict :");
Sb. append (location. getDistrict ());
Sb. append ("\ nStreet :");
Sb. append (location. getStreet ());
Sb. append ("\ naddr :");
Sb. append (location. getAddrStr ());
Sb. append ("\ nDescribe :");
Sb. append (location. getLocationDescribe ());
Sb. append ("\ nDirection (not all devices have value ):");
Sb. append (location. getDirection ());
Sb. append ("\ nPoi :");
If (location. getPoiList ()! = Null &&! Location. getPoiList (). isEmpty ()){
For (int I = 0; I <location. getPoiList (). size (); I ++ ){
Poi poi = (Poi) location. getPoiList (). get (I );
Sb. append (poi. getName () + ";");
}
}
If (location. getLocType () = BDLocation. TypeGpsLocation) {// GPS Positioning Result
Sb. append ("\ nspeed :");
Sb. append (location. getSpeed (); // unit: km/h
Sb. append ("\ nsatellite :");
Sb. append (location. getSatelliteNumber ());
Sb. append ("\ nheight :");
Sb. append (location. getAltitude (); // unit: meters
Sb. append ("\ ndescribe :");
Sb. append ("gps Positioning successful ");
} Else if (location. getLocType () = BDLocation. TypeNetWorkLocation) {// network location result
// Carrier Information
Sb. append ("\ noperationers :");
Sb. append (location. getOperators ());
Sb. append ("\ ndescribe :");
Sb. append ("Network Positioning successful ");
} Else if (location. getLocType () = BDLocation. TypeOffLineLocation) {// offline location result
Sb. append ("\ ndescribe :");
Sb. append ("offline positioning is successful, and offline positioning results are also valid ");
} Else if (location. getLocType () = BDLocation. TypeServerError ){
Sb. append ("\ ndescribe :");
Sb. append ("server network positioning failure, Can feedback IMEI number and big body positioning time to the loc-bugs@baidu.com, someone will trace the cause ");
} Else if (location. getLocType () = BDLocation. TypeNetWorkException ){
Sb. append ("\ ndescribe :");
Sb. append ("locating failed due to different networks. Check whether the network is smooth ");
} Else if (location. getLocType () = BDLocation. TypeCriteriaException ){
Sb. append ("\ ndescribe :");
Sb. append ("location failure due to inability to obtain valid positioning basis, generally due to mobile phone reasons. In flight mode, this result is generally caused. You can try to restart the mobile phone ");
}
LogMsg (sb. toString ());
}
}

};
/**
* Display request strings
*
* @ Param str
*/
Public void logMsg (String str ){
Log. e ("msg", str );
/* Try {
If (LocationResult! = Null)
LocationResult. setText (str );
} Catch (Exception e ){
E. printStackTrace ();
}*/
}

// Three States for map lifecycle management
@ Override
Protected void onResume (){
Super. onResume ();
MMapView. onResume ();
}

@ Override
Protected void onPause (){
Super. onPause ();
MMapView. onPause ();
}

@ Override
Protected void onDestroy (){
MLocationClient. unRegisterLocationListener (mBDLocationListener );
MLocationClient. stop ();
MBaiduMap. setMyLocationEnabled (false );
Super. onDestroy ();
MMapView. onDestroy ();
MMapView = null;
}
}
4. How to calculate the distance between the target address and my location address and the two points
Http://blog.csdn.net/mad1989/article/details/9933089





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.