Recently, there is a function to locate the current city in the project. At the beginning, I chose to use Baidu map api to develop this function. However, according to the code written in the development document provided by Baidu map, the obtained city location is null every time I run. Strangely, I can get the longitude and latitude of the current location again. Therefore, I found many blogs on csdn to find solutions, and found that the obtained city value is still null, Choose Gaode map decisively in the back.
Let's share how I realize this function
Official website of Gaode map developer: http://developer.amap.com/
Development documentation: http://developer.amap.com/api/android-sdk/summary/
1. Go to Gaode developer platform to create applications and create key s
Register a key on the official website of Gaode map developer (Note: a key can only correspond to one project)
SHA1 code acquisition method
packname is the package name of the project
2. Manually add the jar package of Gaode map
stay Gaode map api official website Download the relevant jar package (I have only one location function)
After adding, remember to right-click and select Add As Library to manually add the jar package to the dependent library
3. Create the jniLibs directory under the main directory
Add so Library
Note: only 3D maps need to add so library, and 2D maps do not need this step
4. Androidmanifest in AS Add the corresponding key in the application tag of the XML configuration file
<!-- Cartographic key --> <meta-data android:name="com.amap.api.v2.apikey" android:value="Yours key"> </meta-data>
5. Add corresponding permissions (including static permissions and dynamic permissions)
You can open the permission of the application through another blog: How to enable dynamic permissions
(Note: you can add corresponding permissions according to your own items)
6. Get the current location (do not display the map, only display the current location)
public class MainActivity extends AppCompatActivity { //Declare AMapLocationClient class object AMapLocationClient mLocationClient = null; //Declare AMapLocationClientOption object public AMapLocationClientOption mLocationOption = null; private TextView mTextViewLocation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextViewLocation = findViewById(R.id.textView_location); //Get current location information getCurrentLocationLatLng(); } /** * Initialize location and set location callback listening */ private void getCurrentLocationLatLng() { //Initialize positioning mLocationClient = new AMapLocationClient(getApplicationContext()); //Set location callback listening mLocationClient.setLocationListener(mLocationListener); //Initialize the AMapLocationClientOption object mLocationOption = new AMapLocationClientOption(); // At the same time, network positioning and GPS positioning are used to give priority to returning the positioning results with the highest accuracy and the corresponding address description information mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //Set the positioning interval in milliseconds. The default is 2000ms and the minimum is 1000ms. The default minimum time interval of continuous positioning and cutting is 1000ms mLocationOption.setInterval(3500); //Set positioning parameters for positioning client objects mLocationClient.setLocationOption(mLocationOption); } /** * Locate callback listener */ public AMapLocationListener mLocationListener = new AMapLocationListener() { @Override public void onLocationChanged(AMapLocation amapLocation) { if (amapLocation != null) { if (amapLocation.getErrorCode() == 0) { //Locate the callback information successfully and set relevant messages //Obtain the source of current positioning results, such as network positioning results. See the positioning type table for details amapLocation.getLocationType(); //Get latitude double currentLat = amapLocation.getLatitude(); //Get longitude double currentLon = amapLocation.getLongitude(); //Get current city location mTextViewLocation.setText(amapLocation.getCity()); Log.i("currentLocation", "currentLat : " + currentLat + " currentLon : " + currentLon); //Get accuracy information amapLocation.getAccuracy(); } else { //The error message is displayed. ErrCode is the error code and errInfo is the error message. See the error code table for details. Log.e("AmapError", "location Error, ErrCode:" + amapLocation.getErrorCode() + ", errInfo:" + amapLocation.getErrorInfo()); } } } }; @Override protected void onStart() { super.onStart(); if (mLocationClient != null) { // Start positioning mLocationClient.startLocation(); } } @Override protected void onPause() { super.onPause(); if (mLocationClient != null) { //Stop positioning mLocationClient.stopLocation(); } } @Override protected void onDestroy() { super.onDestroy(); if (mLocationClient != null) { //Destroy location client mLocationClient.onDestroy(); } } }
After completing the above operations, you can obtain the current location information.
reference resources: Official development document of Gaode map