Baidu Map SDK Android development

As a mobile development features, the map function is absolutely indispensable. Next we come through a small project to reach the level of entry.

The first is to apply the key, and then configured into the project, I used development tools are Android Studio, if there is more than one item does not need to know their own Baidu. Then we started working to create their own projects,

We first added to the layout file a TextView control, using text display their location details, this is not difficult. Then we need to exhibition activities in which the hands and feet, how to turn on location services?

We need a LocationClient by instantiating objects, where we need to pass in Context, you can write this, you can also write

getApplicationContext()
And then call it
registerLocationListener(new MyLocationListener())
Parentheses is our registered listener, the listener realizes
BDLocationListener excuse, we need to listeners inside
onReceiveLocation () method to write some logic inside the code, we need to pay attention brackets add BDLocationListener 
objects, in fact DLocationnListener target is to open data carried by the data returned after positioning function, of course, our current data 
and can not read, so again

onReceiveLocation () method which write some logic code, the purpose is to become of us know how to look at the information. 
******** 
then we need to
mLocationClient = new new LocationClient (getApplicationContext ());
 mLocationClient .registerLocationListener ( new new MyLocationListener ());
 register a listener 
added later
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
List<String> permissionList = new ArrayList<>();
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
    permissionList.add(Manifest.permission.READ_PHONE_STATE);
}
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (!permissionList.isEmpty()) {
    String [] permissions = permissionList.toArray(new String[permissionList.size()]);
    ActivityCompat.requestPermissions(MainActivity.this, permissions, 1);
} else {
    requestLocation();//如果所有权限都同意了的Then begin
 
 
requestLocation () method which logic
} // application permissions , run the phone is 6.0 , the introduction of runtime permissions

private void requestLocation() {
}
在这个方法里面我们需要开始我们的第一次定位了,定位的话我们一定需要
LocationClientOption对象的一个实例,在这个对象的基础上设置一些参数
private void requestLocation() {
    LocationClientOption option = new LocationClientOption();
        option.setCoorType("bd09ll");
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setScanSpan(5000);
        option.setIsNeedAddress(true);
        //表示我们需要当前位置的详细信息
        option.setOpenGps(true);
        option.setNeedDeviceDirect(true);
        mLocationClient.setLocOption(option);//最后一定要有这个方法,用来保证前面设置的一些参数可以实现
        //创建一个LocationClientOption对象,并且进行初始化设置,记好最后一定要在
        // onDestory () method will be destroyed to prevent the costs of electricity
    }
After configuration is complete, we do not turn on your location, we just set thepositioning mode, then, we are inside this method
in adding
baiduMap .setMyLocationEnabled ( to true ); // default on positioning
 mLocationClient .start ();
 // Turn on Location, location of the results back into our previous registered listeners inside
Well, currently we positioned to open up.
Positioning of the results of natural to return to our previous registered listeners inside .

Then we need to achieve in the listener inside your stuff :
public class MyLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            StringBuilder currentPosition = new StringBuilder();
            currentPosition.append("纬度:").append(location.getLatitude()).append("\n");
            currentPosition.append("经线:").append(location.getLongitude()).append("\n");
            currentPosition.append("国家:").append(location.getCountry()).append("\n");
           currentPosition.append("省:").append(location.getProvince()).append("\n");            currentPosition.append("市:").append(location.getCity()).append("\n");
           currentPosition.append("区:").append(location.getDistrict()).append("\n");
            currentPosition.append("街道:").append(location.getStreet()).append("\n");
            currentPosition.append("定位方式:");
           if (location.getLocType() == BDLocation.TypeGpsLocation) {
               currentPosition.append("GPS");           } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
              currentPosition.append("网络")}
            positionText.setText(currentPosition);
            
            
        }

    }
Here, we put
 
 
 
 
BDLocation target band data
 
 
 
 
getProvince () or
 
 
 
 
getLongitude () method to replace and so we can see understand information.
This time, we have completed the main positioning function, but we have to remember that positioning is a lot of power work, so we must withdraw from the program
is shut down positioning.
@Override
 protected void onDestroy () {
     Super .onDestroy ();
     mLocationClient .stop ();
     // stop positioned
 }    
    

Document is based on the writings of teacher Guo Lin , playing out of their own understanding, also achieve the effect of mobile phones on their own , perhaps there will be articles written Baidu Map SDK,
if there together science learning children's shoes . Please






Published 37 original articles · won praise 10 · views 10000 +

Guess you like

Origin blog.csdn.net/OneLinee/article/details/78279815
Recommended