国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android之高德地圖定位SDK集成及地圖功能實現

2019-12-12 03:10:08
字體:
來源:轉載
供稿:網友

一:百度高德官方網站,然后去創建應用

網址:http://lbs.amap.com/

1.找到控制臺創建一個應用

2.添加key名稱,注意命名規范,還有就是下面的SHA1和包名

3.點擊右邊的Gradle再選擇signingReport下面會有個命令,稍等幾分鐘得到SHA1

4.添加包名

5.得到key

二:下載定位SDK,下載下來有地圖SDK和定位SDK,然后導入項目,導入再Add As Library,so文件按自己需求來

下載地址:http://lbs.amap.com/api/android-location-sdk/download/

三:在AndroidManifest.xml的application下配置key和注冊service

<!--高德appkey--><meta-data android:name="com.amap.api.v2.apikey" android:value="你的key" /><!--高德service--><service android:name="com.amap.api.location.APSService" />

四:添加權限

<!--高德權限--><!--地圖包、搜索包需要的基礎權限--><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><uses-permission android:name="android.permission.READ_PHONE_STATE" /><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><!--定位包、導航包需要的額外權限(注:基礎權限也需要)--><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /><uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /><uses-permission android:name="android.permission.CHANGE_CONFIGURATION" /><uses-permission android:name="android.permission.WRITE_SETTINGS" />

五:地圖xml布局實現

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout  android:id="@+id/layout_bottom"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:layout_alignParentBottom="true"  android:background="@color/textview_qianhui"  android:orientation="vertical"  android:padding="5dp"  android:visibility="gone">  <TextView   android:id="@+id/map_shop_name"   android:layout_width="match_parent"   android:layout_height="wrap_content"   android:padding="5dp"   android:text="西貝莜面村(豐臺店)"   android:textSize="16sp" />  <TextView   android:id="@+id/map_shop_address"   android:layout_width="match_parent"   android:layout_height="wrap_content"   android:padding="5dp"   android:text="豐葆路,永旺夢樂城四樓" />  <LinearLayout   android:id="@+id/map_shop_goto"   android:layout_width="match_parent"   android:layout_height="wrap_content"   android:layout_gravity="center"   android:gravity="center"   android:orientation="horizontal"   android:padding="10dp">   <TextView    android:id="@+id/map_my_address11"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:background="@color/map_button_color"    android:gravity="center"    android:padding="5dp"    android:text="我的位置"    android:textSize="16sp" />   <TextView    android:id="@+id/map_goto_address11"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_marginLeft="40dp"    android:background="@color/map_button_color"    android:gravity="center"    android:padding="5dp"    android:text="開啟導航"    android:textSize="16sp" />  </LinearLayout> </LinearLayout> <LinearLayout  android:id="@+id/layout_bottom_new"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:layout_alignParentBottom="true"  android:background="@color/map_button_color"  android:orientation="vertical"  android:padding="5dp"  android:visibility="gone">  <TextView   android:id="@+id/tv_getLat"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:layout_weight="1"   android:padding="5dp"   android:text="當前經度:"   android:textSize="16sp" />  <View   android:layout_width="match_parent"   android:layout_height="1dp"   android:background="@color/textview_hui" />  <TextView   android:id="@+id/tv_getLon"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:layout_weight="1"   android:padding="5dp"   android:text="當前緯度:"   android:textSize="16sp" /> </LinearLayout> <com.amap.api.maps.MapView  android:id="@+id/map"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:layout_above="@id/layout_bottom_new"  android:clickable="true"  android:enabled="true" /> <ImageView  android:id="@+id/img_back"  android:layout_width="30dp"  android:layout_height="30dp"  android:layout_marginLeft="10dp"  android:layout_marginTop="20dp"  android:background="@mipmap/map_back" /> <ImageView  android:id="@+id/img_save"  android:layout_width="30dp"  android:layout_height="30dp"  android:layout_alignParentRight="true"  android:layout_marginRight="10dp"  android:layout_marginTop="20dp"  android:background="@mipmap/save"  android:visibility="gone" /> <ImageView  android:id="@+id/map_my_address"  android:layout_width="30dp"  android:layout_height="30dp"  android:layout_alignBottom="@id/map"  android:layout_marginBottom="60dp"  android:layout_marginLeft="10dp"  android:background="@mipmap/my_address" /></RelativeLayout>

六:地圖activity實現

package com.zjtd.bzcommunity.map;import android.graphics.BitmapFactory;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.ImageView;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.MapView;import com.amap.api.maps.model.BitmapDescriptor;import com.amap.api.maps.model.BitmapDescriptorFactory;import com.amap.api.maps.model.LatLng;import com.amap.api.maps.model.MarkerOptions;import com.zjtd.bzcommunity.R;import java.text.SimpleDateFormat;import java.util.Date;/** * Created by Administrator on 2017/3/23. * 高德地圖 */public class ShowMapActivity extends AppCompatActivity implements View.OnClickListener { private MapView mapView;//地圖控件 private ImageView img_back;//返回鍵 private ImageView map_my_address;//復位 private AMap aMap;//地圖控制器對象 //聲明AMapLocationClient類對象 public AMapLocationClient mLocationClient = null; //聲明mLocationOption對象 public AMapLocationClientOption mLocationOption = null; private double lat; private double lon; @Override protected void onCreate(@Nullable Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.map);  initlayout();  //初始化定位  mLocationClient = new AMapLocationClient(getApplicationContext());  //設置定位回調監聽  mLocationClient.setLocationListener(mLocationListener);  //必須要寫  mapView.onCreate(savedInstanceState);  init(); } /**  * 實例化  */ private void initlayout() {  mapView = (MapView) findViewById(R.id.map);  img_back = (ImageView) findViewById(R.id.img_back);  map_my_address = (ImageView) findViewById(R.id.map_my_address);  img_back.setOnClickListener(this);  map_my_address.setOnClickListener(this); } /**  * * 初始化AMap對象  */ private void init() {  if (aMap == null) {   aMap = mapView.getMap();  }  setUpMap(); } /**  * 配置定位參數  */ private void setUpMap() {  //初始化定位參數  mLocationOption = new AMapLocationClientOption();  //設置定位模式為高精度模式,Battery_Saving為低功耗模式,Device_Sensors是僅設備模式  mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);  //設置是否返回地址信息(默認返回地址信息)  mLocationOption.setNeedAddress(true);  //設置是否只定位一次,默認為false  mLocationOption.setOnceLocation(false);  //設置是否強制刷新WIFI,默認為強制刷新  mLocationOption.setWifiActiveScan(true);  //設置是否允許模擬位置,默認為false,不允許模擬位置  mLocationOption.setMockEnable(false);  //設置定位間隔,單位毫秒,默認為2000ms  mLocationOption.setInterval(2000);  //給定位客戶端對象設置定位參數  mLocationClient.setLocationOption(mLocationOption);  //啟動定位  mLocationClient.startLocation(); } /**  * 聲明定位回調監聽器  */ public AMapLocationListener mLocationListener = new AMapLocationListener() {  @Override  public void onLocationChanged(AMapLocation amapLocation) {   if (amapLocation != null) {    if (amapLocation.getErrorCode() == 0) {     //定位成功回調信息,設置相關消息     amapLocation.getLocationType();//獲取當前定位結果來源,如網絡定位結果,詳見定位類型表     amapLocation.getLatitude();//獲取緯度     amapLocation.getLongitude();//獲取經度     amapLocation.getAccuracy();//獲取精度信息     SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");     Date date = new Date(amapLocation.getTime());     df.format(date);//定位時間     amapLocation.getAddress();//地址,如果option中設置isNeedAddress為false,則沒有此結果,網絡定位結果中會有地址信息,GPS定位不返回地址信息。     amapLocation.getCountry();//國家信息     amapLocation.getProvince();//省信息     amapLocation.getCity();//城市信息     amapLocation.getDistrict();//城區信息     amapLocation.getStreet();//街道信息     amapLocation.getStreetNum();//街道門牌號信息     amapLocation.getCityCode();//城市編碼     amapLocation.getAdCode();//地區編碼     amapLocation.getAoiName();//獲取當前定位點的AOI信息     lat = amapLocation.getLatitude();     lon = amapLocation.getLongitude();     Log.v("pcw", "lat : " + lat + " lon : " + lon);     Log.v("pcw", "Country : " + amapLocation.getCountry() + " province : " + amapLocation.getProvince() + " City : " + amapLocation.getCity() + " District : " + amapLocation.getDistrict());     //清空緩存位置     aMap.clear();     // 設置當前地圖顯示為當前位置     aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon), 19));     MarkerOptions markerOptions = new MarkerOptions();     markerOptions.position(new LatLng(lat, lon));     markerOptions.title("當前位置");     markerOptions.visible(true);     BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.btn_kuaisong_change_icon));     markerOptions.icon(bitmapDescriptor);     aMap.addMarker(markerOptions);    } else {     //顯示錯誤信息ErrCode是錯誤碼,errInfo是錯誤信息,詳見錯誤碼表。     Log.e("AmapError", "location Error, ErrCode:"       + amapLocation.getErrorCode() + ", errInfo:"       + amapLocation.getErrorInfo());    }   }  } }; /**  * 重新繪制加載地圖  */ @Override protected void onResume() {  super.onResume();  mapView.onResume(); } /**  * 暫停地圖的繪制  */ @Override protected void onPause() {  super.onPause();  mapView.onPause(); } /**  * 保存地圖當前的狀態方法必須重寫  */ @Override protected void onSaveInstanceState(Bundle outState) {  super.onSaveInstanceState(outState);  mapView.onSaveInstanceState(outState); } /**  * 銷毀地圖  */ @Override protected void onDestroy() {  super.onDestroy();  mapView.onDestroy(); } @Override public void onClick(View view) {  switch (view.getId()) {   case R.id.img_back:    finish();    break;  } }}

效果圖:

七:定位功能實現

1.效果圖

2.activity的實現

public class LoginAndRegisterActivity extends Activity implements OnClickListener { /**  * 登錄界面  */ private EditText et_login_phoneNumber;//手機號 private EditText et_login_password;//密碼 private RelativeLayout btn_login;//登錄 private TextView tv_forget;//忘記密碼 private TextView textzc;//注冊 private ImageView imagqk;//清空密碼 private String address;//地址 private LinearLayout lineargg;//隨便逛逛 private ImageView fanhuicc;//返回鍵Register //聲明AMapLocationClient類對象 public AMapLocationClient mLocationClient = null; //聲明定位回調監聽器 //可以通過類implement方式實現AMapLocationListener接口,也可以通過創造接口類對象的方法實現 public AMapLocationListener mLocationListener = new AMapLocationListener() {  @Override  public void onLocationChanged(AMapLocation amapLocation) {   if (amapLocation != null) {    if (amapLocation.getErrorCode() == 0) {     //可在其中解析amapLocation獲取相應內容。     amapLocation.getLocationType();//獲取當前定位結果來源,如網絡定位結果,詳見定位類型表     amapLocation.getLatitude();//獲取緯度     amapLocation.getLongitude();//獲取經度     amapLocation.getAccuracy();//獲取精度信息     amapLocation.getAddress();//地址,如果option中設置isNeedAddress為false,則沒有此結果,網絡定位結果中會有地址信息,GPS定位不返回地址信息。     amapLocation.getCountry();//國家信息     amapLocation.getProvince();//省信息     amapLocation.getCity();//城市信息     amapLocation.getDistrict();//城區信息     amapLocation.getStreet();//街道信息     amapLocation.getStreetNum();//街道門牌號信息     amapLocation.getCityCode();//城市編碼     amapLocation.getAdCode();//地區編碼     amapLocation.getAoiName();//獲取當前定位點的AOI信息     amapLocation.getBuildingId();//獲取當前室內定位的建筑物Id     amapLocation.getFloor();//獲取當前室內定位的樓層//     amapLocation.getGpsStatus();//獲取GPS的當前狀態     //獲取定位時間     SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");     Date date = new Date(amapLocation.getTime());     df.format(date);     address = amapLocation.getAddress();    } else {     //定位失敗時,可通過ErrCode(錯誤碼)信息來確定失敗的原因,errInfo是錯誤信息,詳見錯誤碼表。     Log.e("AmapError", "location Error, ErrCode:"       + amapLocation.getErrorCode() + ", errInfo:"       + amapLocation.getErrorInfo());    }   }  } }; //聲明AMapLocationClientOption對象 public AMapLocationClientOption mLocationOption = null; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.fragment_login);  initViews();  init(); } /**  * 實例化  */ private void initViews() {  et_login_phoneNumber = (EditText) findViewById(R.id.et_login_phoneNumber);  et_login_password = (EditText) findViewById(R.id.et_login_password);  btn_login = (RelativeLayout) findViewById(R.id.btn_login);  tv_forget = (TextView) findViewById(R.id.tv_forget);  textzc = (TextView) findViewById(R.id.textzc);  imagqk = (ImageView) findViewById(R.id.imagqk);  lineargg = (LinearLayout) findViewById(R.id.lineargg);  fanhuicc = (ImageView) findViewById(R.id.fanhuicc);  btn_login.setOnClickListener(this);  tv_forget.setOnClickListener(this);  textzc.setOnClickListener(this);  imagqk.setOnClickListener(this);  lineargg.setOnClickListener(this);  fanhuicc.setOnClickListener(this); } private void init() {  //初始化定位  mLocationClient = new AMapLocationClient(getApplicationContext());  //設置定位回調監聽  mLocationClient.setLocationListener(mLocationListener);  //初始化AMapLocationClientOption對象  mLocationOption = new AMapLocationClientOption();  //設置定位模式為AMapLocationMode.Hight_Accuracy,高精度模式。  mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);  //設置是否返回地址信息(默認返回地址信息)  mLocationOption.setNeedAddress(true);  //設置是否只定位一次,默認為false  mLocationOption.setOnceLocation(false);  //設置是否強制刷新WIFI,默認為強制刷新  mLocationOption.setWifiActiveScan(true);  //設置是否允許模擬位置,默認為false,不允許模擬位置  mLocationOption.setMockEnable(false);  //設置定位間隔,單位毫秒,默認為2000ms  mLocationOption.setInterval(2000);  //給定位客戶端對象設置定位參數  mLocationClient.setLocationOption(mLocationOption);  //單位是毫秒,默認30000毫秒,建議超時時間不要低于8000毫秒。  mLocationOption.setHttpTimeOut(20000);  //關閉緩存機制  mLocationOption.setLocationCacheEnable(false);  //啟動定位  mLocationClient.startLocation(); }@Overrideprotected void onStop() { super.onStop(); mLocationClient.stopLocation();//停止定位 }/** * 方法必須重寫  */@Overrideprotected void onDestroy() { super.onDestroy(); mLocationClient.onDestroy();//銷毀定位客戶端。 }

到此定位和地圖顯示功能已實現,大神勿噴,有用的可以借鑒一下。

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 德兴市| 布尔津县| 德庆县| 聊城市| 固安县| 怀安县| 雷波县| 朔州市| 万源市| 保靖县| 内黄县| 江口县| 灵台县| 修水县| 宜都市| 钟山县| 白水县| 汽车| 安宁市| 泾阳县| 肇州县| 竹山县| 清远市| 黑水县| 公安县| 阿图什市| 漾濞| 苍梧县| 依兰县| 来凤县| 安远县| 大悟县| 杭州市| 新乡县| 晋江市| 密云县| 新宁县| 平度市| 宜宾县| 博乐市| 高淳县|