2021@SDUSC
Realization of positioning function
private LocationClient mLocationClient = null; private MyLocationConfiguration.LocationMode locationMode; private MyLocationConfiguration.LocationMode mLocationMode;
For the first time, you need to make preparations and do not configure the Android positioning SDK of Baidu map. You can obtain the key according to the tutorial on the official website and configure the environment in AndroidManifest.xml and the build.gradle file of the main project.
Then, the LocationClient class object needs to be declared in the main thread, and the Context type parameter needs to be passed in to initialize the object. For example, use the getApplicationConext() method to obtain a valid Context for the whole process.
LocationClient class is the client of the location service. The host program declares this class on the client and calls it. At present, it only supports starting in the main thread.
public class MyLocationListener extends BDAbstractLocationListener { @Override public void onReceiveLocation(BDLocation location) { //After mapView is destroyed, it is not in the location where new reception is processed if (location == null || mMapView == null){ return; } LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); if (isFirstLocate) { isFirstLocate = false; MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll); mBaiduMap.animateMapStatus(update); update = MapStatusUpdateFactory.zoomTo(18f); mBaiduMap.animateMapStatus(update); } MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) .direction(location.getDirection()).latitude(location.getLatitude()) .longitude(location.getLongitude()).build(); mBaiduMap.setMyLocationData(locData); } }
Abstract type listening interface BDAbstractLocationListener is used to implement location listening. This interface will asynchronously obtain location results. onReceiveLocation(BDLocation location) is the location request callback function. The BDLocation class here is the positioning result information class. All positioning related results can be obtained through its various get methods.
The LatLng class is the basic data structure of geographical coordinates. location.getLatitude() gets the latitude coordinates and location.getLongitude() gets the longitude coordinates.
The author sets a variable to judge whether the user locates for the first time. If it is to change the isFirstLocate state to false, the rest will not be the first time after the first execution, and then set some display states of the map, such as what position is the center of the display, and how many zoom maps the user displays. The MapStatusUpdate class describes the changes that will occur in the map status. Mapstatusupdatefactory is used to generate changes to the map status. MapStatusUpdateFactory.newLatLng(ll); Is to set the currently obtained positioning as the new center point of the map. mBaiduMap.animateMapStatus(update); The map change status just defined can be updated by animation, that is, the map is changed to be displayed centered on the user's current positioning. The default animation takes 300ms. Later, set the map zoom level, use the method zoomTo(18f) of the map status factory class, change the update variable from the setting center position to set the map zoom level according to the value 18f given by the method, and then update the map mbaidumap status again.
The operations to be performed regardless of the first positioning are as follows. MyLocationData is a positioning data class, which can include positioning accuracy, direction during GPS positioning, baidu latitude coordinate, baidu longitude coordinate, number of satellites during GPS positioning (satellites Num) and GPS positioning speed information (speed). MyLocationData.Builder is a positioning data builder and a nested class of MyLocationData. Positioning data objects can be built for it through the method build(). location.getRadius() is to obtain the accuracy of the current location. The default value is 0.0f. location.getDirection() is the direction of travel when obtaining GPS positioning results, in degrees. According to the accuracy of the current positioning, the author uses the positioning data builder to build attribute positioning accuracy, GPS positioning direction, baidu latitude and Baidu longitude for the new positioning data object. Finally, setMyLocationData(locData)
Set the positioning data object defined above. This method will not take effect until the positioning layer is allowed first.
Button listener
public void onClick(View v){ if(v.getId() == R.id.btn_library){ mIntent = new Intent(MainActivity.this, DescriptionActivity.class); //Use Bundle to pass Int type data Bundle bundle = new Bundle(); bundle.putInt("name", library_info.getName()); bundle.putInt("des",library_info.getDescreption()); bundle.putInt("picname", library_info.getPicname()); mIntent.putExtras(bundle); startActivity(mIntent); }else if(v.getId() == R.id.btn_tech){ mIntent = new Intent(MainActivity.this, DescriptionActivity.class); Bundle bundle = new Bundle(); bundle.putInt("name", tech_info.getName()); bundle.putInt("des",tech_info.getDescreption()); bundle.putInt("picname", tech_info.getPicname()); mIntent.putExtras(bundle); startActivity(mIntent); }else if(v.getId() == R.id.btn_dining){ mIntent = new Intent(MainActivity.this, DescriptionActivity.class); Bundle bundle = new Bundle(); bundle.putInt("name", dining_info.getName()); bundle.putInt("des",dining_info.getDescreption()); bundle.putInt("picname", dining_info.getPicname()); mIntent.putExtras(bundle); startActivity(mIntent); } }