a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統(tǒng) > Android > 正文

Android之高德地圖定位SDK集成及地圖功能實(shí)現(xiàn)

2019-12-12 03:10:08
字體:
供稿:網(wǎng)友

一:百度高德官方網(wǎng)站,然后去創(chuàng)建應(yīng)用

網(wǎng)址:http://lbs.amap.com/

1.找到控制臺(tái)創(chuàng)建一個(gè)應(yīng)用

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

3.點(diǎn)擊右邊的Gradle再選擇signingReport下面會(huì)有個(gè)命令,稍等幾分鐘得到SHA1

4.添加包名

5.得到key

二:下載定位SDK,下載下來有地圖SDK和定位SDK,然后導(dǎo)入項(xiàng)目,導(dǎo)入再Add As Library,so文件按自己需求來

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

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

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

四:添加權(quán)限

<!--高德權(quán)限--><!--地圖包、搜索包需要的基礎(chǔ)權(quán)限--><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" /><!--定位包、導(dǎo)航包需要的額外權(quán)限(注:基礎(chǔ)權(quán)限也需要)--><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布局實(shí)現(xiàn)

<?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="西貝莜面村(豐臺(tái)店)"   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="豐葆路,永旺夢(mèng)樂城四樓" />  <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="開啟導(dǎo)航"    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="當(dāng)前經(jīng)度:"   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="當(dāng)前緯度:"   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實(shí)現(xiàn)

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;//復(fù)位 private AMap aMap;//地圖控制器對(duì)象 //聲明AMapLocationClient類對(duì)象 public AMapLocationClient mLocationClient = null; //聲明mLocationOption對(duì)象 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());  //設(shè)置定位回調(diào)監(jiān)聽  mLocationClient.setLocationListener(mLocationListener);  //必須要寫  mapView.onCreate(savedInstanceState);  init(); } /**  * 實(shí)例化  */ 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對(duì)象  */ private void init() {  if (aMap == null) {   aMap = mapView.getMap();  }  setUpMap(); } /**  * 配置定位參數(shù)  */ private void setUpMap() {  //初始化定位參數(shù)  mLocationOption = new AMapLocationClientOption();  //設(shè)置定位模式為高精度模式,Battery_Saving為低功耗模式,Device_Sensors是僅設(shè)備模式  mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);  //設(shè)置是否返回地址信息(默認(rèn)返回地址信息)  mLocationOption.setNeedAddress(true);  //設(shè)置是否只定位一次,默認(rèn)為false  mLocationOption.setOnceLocation(false);  //設(shè)置是否強(qiáng)制刷新WIFI,默認(rèn)為強(qiáng)制刷新  mLocationOption.setWifiActiveScan(true);  //設(shè)置是否允許模擬位置,默認(rèn)為false,不允許模擬位置  mLocationOption.setMockEnable(false);  //設(shè)置定位間隔,單位毫秒,默認(rèn)為2000ms  mLocationOption.setInterval(2000);  //給定位客戶端對(duì)象設(shè)置定位參數(shù)  mLocationClient.setLocationOption(mLocationOption);  //啟動(dòng)定位  mLocationClient.startLocation(); } /**  * 聲明定位回調(diào)監(jiān)聽器  */ public AMapLocationListener mLocationListener = new AMapLocationListener() {  @Override  public void onLocationChanged(AMapLocation amapLocation) {   if (amapLocation != null) {    if (amapLocation.getErrorCode() == 0) {     //定位成功回調(diào)信息,設(shè)置相關(guān)消息     amapLocation.getLocationType();//獲取當(dāng)前定位結(jié)果來源,如網(wǎng)絡(luò)定位結(jié)果,詳見定位類型表     amapLocation.getLatitude();//獲取緯度     amapLocation.getLongitude();//獲取經(jīng)度     amapLocation.getAccuracy();//獲取精度信息     SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");     Date date = new Date(amapLocation.getTime());     df.format(date);//定位時(shí)間     amapLocation.getAddress();//地址,如果option中設(shè)置isNeedAddress為false,則沒有此結(jié)果,網(wǎng)絡(luò)定位結(jié)果中會(huì)有地址信息,GPS定位不返回地址信息。     amapLocation.getCountry();//國家信息     amapLocation.getProvince();//省信息     amapLocation.getCity();//城市信息     amapLocation.getDistrict();//城區(qū)信息     amapLocation.getStreet();//街道信息     amapLocation.getStreetNum();//街道門牌號(hào)信息     amapLocation.getCityCode();//城市編碼     amapLocation.getAdCode();//地區(qū)編碼     amapLocation.getAoiName();//獲取當(dāng)前定位點(diǎn)的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();     // 設(shè)置當(dāng)前地圖顯示為當(dāng)前位置     aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon), 19));     MarkerOptions markerOptions = new MarkerOptions();     markerOptions.position(new LatLng(lat, lon));     markerOptions.title("當(dāng)前位置");     markerOptions.visible(true);     BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.btn_kuaisong_change_icon));     markerOptions.icon(bitmapDescriptor);     aMap.addMarker(markerOptions);    } else {     //顯示錯(cuò)誤信息ErrCode是錯(cuò)誤碼,errInfo是錯(cuò)誤信息,詳見錯(cuò)誤碼表。     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(); } /**  * 保存地圖當(dāng)前的狀態(tài)方法必須重寫  */ @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;  } }}

效果圖:

七:定位功能實(shí)現(xiàn)

1.效果圖

2.activity的實(shí)現(xiàn)

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

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

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持武林網(wǎng)!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 在线免费色视频 | 国产精品久久国产精品 | 国产视频久久久 | 中文字幕av网 | 欧美日韩亚洲国产 | 草草网| 国产丝袜一区二区三区免费视频 | 伦理一区| 黄色小视频在线观看 | 91在线看视频 | 日韩中文字幕在线看 | 久久精品国产99 | xxxwww日本| 婷婷综合激情 | 91精品久久久久久综合五月天 | 色www精品视频在线观看 | 黄色av观看 | 91精产国品一二三区在线观看 | 激情开心成人网 | 久久狠 | 91久久人人夜色一区二区 | 国产精品视频一区在线观看 | 国内精品久久久久久久97牛牛 | 久久精品久久精品国产大片 | 欧美日韩国产一区二区三区不卡 | 日日夜夜天天 | 欧美区日韩区 | 国产精品一区亚洲二区日本三区 | 欧美一区二区三区精品 | 精品美女在线观看视频在线观看 | 欧美成人一区二区 | 在线观看日韩av | 国产成人精品一区二区三区网站观看 | 国产精品污www在线观看 | 国产三级自拍 | 亚洲精品欧美视频 | 久久久久久久久国产成人免费 | 九九免费视频 | 成人日韩| 国产精品一区亚洲二区日本三区 | 国产精品久久久久久福利一牛影视 |