Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Android to realize Amap display and location

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to use Android to achieve Amap display and positioning. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. To realize the positioning of Amap, we must first download Amap's SDK.

Then log in to your registered account on Amap's official website and go to the console-- > Application Management-- > add a new key to get your own key value and the official website address of the specific method of obtaining key.

2. Extract the .jar file and put it in the libs file, and manually add it to the dependency library (right Add--) 3. If your map SDK is 3D, create a jniLibs folder in main and put the other files you unzipped here. 4. The development environment has been configured, and the next step is to hit the code.

Add key and Service to the project's "AndroidManifest.xml" manifest file

Add permission

Acticity_main.xml layout file

MainActivity code

Import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.widget.Toast; import com.amap.api.location.AMapLocation;import com.amap.api.location.AMapLocationClient;import com.amap.api.location.AMapLocationClientOption;import com.amap.api.location.AMapLocationListener;import com.amap.api.maps.AMap;import com.amap.api.maps.CameraUpdateFactory;import com.amap.api.maps.LocationSource;import com.amap.api.maps.MapView;import com.amap.api.maps.UiSettings Import com.amap.api.maps.model.LatLng; import java.text.SimpleDateFormat;import java.util.Date; public class MainActivity extends AppCompatActivity implements LocationSource, AMapLocationListener {/ / AMap is the map object private AMap aMap; private MapView mapView; / / declares the AMapLocationClient class object, locates the initiator private AMapLocationClient mLocationClient = null; / / declares the mLocationOption object, the positioning parameter public AMapLocationClientOption mLocationOption = null; / / declares the mListener object, and locates the listener private OnLocationChangedListener mListener = null / / identification, which is used to determine whether the location information is displayed only once and whether the user relocates private boolean isFirstLoc = true; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); / / gets the map control reference mapView = (MapView) findViewById (R.id.map); / / executes mMapView.onCreate (savedInstanceState) when activity executes onCreate to implement map life cycle management mapView.onCreate (savedInstanceState) If (aMap = = null) {aMap = mapView.getMap (); / / set to display the positioning button and click UiSettings settings = aMap.getUiSettings (); aMap.setLocationSource (this); / / set the monitor for positioning / / whether to display the positioning button settings.setMyLocationButtonEnabled (true); aMap.setMyLocationEnabled (true); / / display the positioning layer and trigger positioning. The default is flase} / / start positioning location () } private void location () {/ / initialize positioning mLocationClient = new AMapLocationClient (getApplicationContext ()); / / set positioning callback listening mLocationClient.setLocationListener (this); / / initialize positioning parameters mLocationOption = new AMapLocationClientOption (); / / set positioning mode to Hight_Accuracy high-precision mode, Battery_Saving to low power mode, Device_Sensors to device-only mode mLocationOption.setLocationMode (AMapLocationClientOption.AMapLocationMode.Hight_Accuracy) / / set whether to return address information (default returned address information) mLocationOption.setNeedAddress (true); / / set whether to locate only once, default is false mLocationOption.setOnceLocation (false); / / set whether to force WIFI refresh, default is forced refresh mLocationOption.setWifiActiveScan (true); / / set whether to allow simulation location, default is false, do not allow simulation location mLocationOption.setMockEnable (false) / / set positioning interval (in milliseconds), default is 2000ms mLocationOption.setInterval (2000); / / set positioning parameters mLocationClient.setLocationOption (mLocationOption) for positioning client objects; / / start positioning mLocationClient.startLocation ();} @ Override protected void onDestroy () {super.onDestroy (); / / execute mMapView.onDestroy () when activity executes onDestroy to achieve map life cycle management mapView.onDestroy (); mLocationClient.stopLocation (); / / stop positioning mLocationClient.onDestroy () / / destroy the location client. } @ Override protected void onResume () {super.onResume (); / / execute mMapView.onResume () when activity executes onResume to implement map life cycle management mapView.onResume ();} @ Override protected void onPause () {super.onPause (); / / execute mMapView.onPause () when activity executes onPause to implement map life cycle management mapView.onPause ();} @ Override protected void onSaveInstanceState (Bundle outState) {super.onSaveInstanceState (outState) / execute mMapView.onSaveInstanceState (outState) when activity executes onSaveInstanceState to achieve map life cycle management mapView.onSaveInstanceState (outState);} @ Override public void onLocationChanged (AMapLocation aMapLocation) {if (aMapLocation! = null) {if (aMapLocation.getErrorCode () = = 0) {/ / locate the successful callback information and set the relevant message aMapLocation.getLocationType (); / / get the current location result source, such as network location result. For more information, please see aMapLocation.getLatitude () / / get latitude aMapLocation.getLongitude (); / / get longitude aMapLocation.getAccuracy (); / get precision information SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); Date date = new Date (aMapLocation.getTime ()); df.format (date); / / location time aMapLocation.getAddress (); / / address. If isNeedAddress is set to false in option, there will be address information in the network location result, and GPS location will not return address information. AMapLocation.getCountry (); / / National Information aMapLocation.getProvince (); / / Provincial Information aMapLocation.getCity (); / / City Information aMapLocation.getDistrict (); / / City Information aMapLocation.getStreet (); / / Street Information aMapLocation.getStreetNum (); / / Street number Information aMapLocation.getCityCode (); / / City Code aMapLocation.getAdCode () / / area code / / if the flag bit is not set, when you drag the map at this time, it will continue to move the map to the current position if (isFirstLoc) {/ / set the zoom level aMap.moveCamera (CameraUpdateFactory.zoomTo (17)); / / move the map to the anchor point aMap.moveCamera (CameraUpdateFactory.changeLatLng (new LatLng (aMapLocation.getLatitude (), aMapLocation.getLongitude () / / Click the positioning button to move the center of the map to the anchor point mListener.onLocationChanged (aMapLocation); / / add pushpins / / aMap.addMarker (getMarkerOptions (amapLocation)); / / get positioning information StringBuffer buffer = new StringBuffer () Buffer.append (aMapLocation.getCountry () + "+ aMapLocation.getProvince () +" + aMapLocation.getCity () + "+ aMapLocation.getProvince () +" + aMapLocation.getDistrict () + "+ aMapLocation.getStreet () +" + aMapLocation.getStreetNum ()); Toast.makeText (getApplicationContext (), buffer.toString (), Toast.LENGTH_LONG). Show (); isFirstLoc = false;} else {/ / shows that ErrCode is an error code, errInfo is an error message, see the error code table for details. Log.e ("AmapError", "location Error, ErrCode:" + aMapLocation.getErrorCode () + ", errInfo:" + aMapLocation.getErrorInfo ()); Toast.makeText (getApplicationContext (), "location failure", Toast.LENGTH_LONG). Show ();} @ Override public void activate (OnLocationChangedListener onLocationChangedListener) {mListener = onLocationChangedListener;} @ Override public void deactivate () {mListener = null;}}

On "how to use Android to achieve Amap display and positioning" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report