一,下載sdk --代開2Dmap_Demo
二,獲取appkey(先要獲取keystore的shaw1值)
(1),在C:\Users\Administrator.android目錄下找到debug.keystore,
(2,接著在該目錄下的地址欄輸入cmd命令
Keytool -v -list -keystore debug.keystore的絕對路徑
(3,回車-->輸入密碼(密碼是android)
(4,復制
三,導入jar包,并添加到依賴庫(將2D地圖demo的jar拷貝到項目)
四,拷貝Demo的清單文件
五,copy高德sdk的Demo的代碼
六,地圖頁面的代碼:
public class SecondLocationActivity extends AppCompatActivityimplements LocationSource, AMapLocationListener, PoiSearch.OnPoiSearchListener, View.OnClickListener {
private AMap aMap;
private OnLocationChangedListener mListener;
private AMapLocationClient mlocationClient;
private AMapLocationClientOption mLocationOption;
private int currentPage;
private String keyWord = "";
private String city = "深圳市";
private PoiSearch.Query query;
private PoiSearch poiSearch;
private LatLng lp;
private PoiResult poiResult;
private ArrayList<PoiItem> poiItems;
private AroundRvAdapter mAroundRvAdapter;
private ActivitySecondLocationBinding mBinding;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_second_location);
mBinding.mapView.onCreate(savedInstanceState);
mBinding.recycleView.setLayoutManager(new LinearLayoutManager(this));
mAroundRvAdapter = new AroundRvAdapter(this);
mBinding.recycleView.setAdapter(mAroundRvAdapter);
intiUI();
}
private void intiUI() {
if (aMap == null) {
aMap = mBinding.mapView.getMap();
setUpMap();
}
}
/**
* 設置一些amap的屬性
*/
private void setUpMap() {
// 自定義系統定位小藍點
MyLocationStyle myLocationStyle = new MyLocationStyle();
myLocationStyle.myLocationIcon(BitmapDescriptorFactory
.fromResource(R.drawable.location_marker));// 設置小藍點的圖標
myLocationStyle.strokeColor(Color.BLACK);// 設置圓形的邊框顏色
myLocationStyle.radiusFillColor(Color.argb(100, 0, 0, 180));// 設置圓形的填充顏色
// myLocationStyle.anchor(int,int)//設置小藍點的錨點
myLocationStyle.strokeWidth(1.0f);// 設置圓形的邊框粗細
aMap.setMyLocationStyle(myLocationStyle);
aMap.setLocationSource(this);// 設置定位監聽
aMap.getUiSettings().setMyLocationButtonEnabled(true);// 設置默認定位按鈕是否顯示
aMap.setMyLocationEnabled(true);// 設置為true表示顯示定位層并可觸發定位,false表 示隱藏定位層并不可觸發定位,默認是false
// aMap.setMyLocationType()
}
/**
* 方法必須重寫,關聯生命周期,減少消耗,提高性能
*/
@Override
protected void onResume() {
super.onResume();
mBinding.mapView.onResume();
}
/**
* 方法必須重寫,關聯生命周期,減少消耗,提高性能
*/
@Override
protected void onPause() {
super.onPause();
mBinding.mapView.onPause();
}
/**
* 方法必須重寫,關聯生命周期,減少消耗,提高性能
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mBinding.mapView.onSaveInstanceState(outState);
}
/**
* 方法必須重寫,關聯生命周期,減少消耗,提高性能
*/
@Override
protected void onDestroy() {
super.onDestroy();
mBinding.mapView.onDestroy();
}
/**
* 激活定位,激活以后才能夠定位
*/
@Override
public void activate(OnLocationChangedListener listener) {
mListener = listener;
if (mlocationClient == null) {
mlocationClient = new AMapLocationClient(this);
mLocationOption = new AMapLocationClientOption();
//設置定位監聽
mlocationClient.setLocationListener(this);
//設置為高精度定位模式
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//設置定位參數
mlocationClient.setLocationOption(mLocationOption);
// 此方法為每隔固定時間會發起一次定位請求,為了減少電量消耗或網絡流量消耗,
// 注意設置合適的定位時間的間隔(最小間隔支持為2000ms),并且在合適時間調用stopLocation()方法來取消定位請求
// 在定位結束后,在合適的生命周期調用onDestroy()方法
// 在單次定位情況下,定位無論成功與否,都無需調用stopLocation()方法移除請求,定位sdk內部會移除
mlocationClient.startLocation();
}
}
/**
* 停止定位
*/
@Override
public void deactivate() {
mListener = null;
if (mlocationClient != null) {
mlocationClient.stopLocation();
mlocationClient.onDestroy();
}
mlocationClient = null;
}
/**
* 定位成功后回調函數
*/
@Override
public void onLocationChanged(AMapLocation amapLocation) {
if (mListener != null && amapLocation != null) {
if (amapLocation != null
&& amapLocation.getErrorCode() == 0) {
// 1,顯示系統小藍點
mListener.onLocationChanged(amapLocation);
lp = new LatLng(amapLocation.getLatitude(), amapLocation.getLongitude());
// aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(amapLocation.getLatitude(),amapLocation.getLongitude())));
aMap.moveCamera(CameraUpdateFactory.changeLatLng(lp));
aMap.moveCamera(CameraUpdateFactory.zoomTo(19)); //范圍在3-20之間
Log.e("date", "搜索結果:" + amapLocation.getAddress());
//2,只定位一次,就終止(為了提高性能)
if (mlocationClient != null) {
mlocationClient.stopLocation();
mlocationClient.onDestroy();
}
//3,嘗試搜索周邊,搜索所有poi點,500米內
doSearchQuery();
} else {
String errText = "定位失敗," + amapLocation.getErrorCode() + ": " + amapLocation.getErrorInfo();
Log.e("AmapErr", errText);
}
}
}
protected void doSearchQuery() {
currentPage = 0;
// 第一個參數表示搜索字符串,第二個參數表示poi搜索類型,第三個參數表示poi搜索區域(空字符串代表全國)
query = new PoiSearch.Query(keyWord, "", city);
query.setPageSize(50);// 設置每頁最多返回多少條poiitem
query.setPageNum(currentPage);// 設置查第一頁
if (lp != null) {
poiSearch = new PoiSearch(this, query);
poiSearch.setOnPoiSearchListener(this);
poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(lp.latitude, lp.longitude), 5000, true));//200米范圍內
// 設置搜索區域為以lp點為圓心,其周圍5000米范圍
poiSearch.searchPOIAsyn();// 異步搜索
}
}
@Override
public void onPoiSearched(PoiResult result, int rcode) {
if (rcode == 1000) {
if (result != null && result.getQuery() != null) {// 搜索poi的結果
if (result.getQuery().equals(query)) {// 是否是同一條
poiResult = result;
poiItems = poiResult.getPois();// 取得第一頁的poiitem數據,頁數從數字0開始
Toast.makeText(this, "一共有" + poiItems.size() + "個結果", Toast.LENGTH_SHORT).show();
mAroundRvAdapter.setPoiItemList(poiItems);
}
}
}
}
@Override
public void onPoiItemSearched(PoiItem poiItem, int i) {
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.rl_exit:
finish();
if(Integer.valueOf(android.os.Build.VERSION.SDK) >= 5) {
overridePendingTransition(0,0); //2個0表示activity切換不需要動畫效果
}
break;
}
}
}
七,適配器
public class AroundRvAdapter extends RecyclerView.Adapter {
private Context mContext;
public AroundRvAdapter(Context context) {
mContext = context;
}
private List<PoiItem> mPoiItemList = new ArrayList<>();
public void setPoiItemList(List<PoiItem> poiItemList) {
mPoiItemList = poiItemList;
notifyDataSetChanged();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_around, parent, false);
ViewHolder viewHolder = new ViewHolder(itemView);
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ViewHolder viewHolder = (ViewHolder) holder;
viewHolder.setData(mPoiItemList.get(position));
}
@Override
public int getItemCount() {
if(mPoiItemList!=null){
return mPoiItemList.size();
}
return 0;
}
class ViewHolder extends RecyclerView.ViewHolder{
TextView mTitle;
TextView mAddress;
ViewHolder(View view) {
super(view);
mTitle = view.findViewById(R.id.title);
mAddress = view.findViewById(R.id.address);
}
public void setData(PoiItem poiItem) {
mTitle.setText(poiItem.getTitle());
mAddress.setText(poiItem.getSnippet()); //摘要信息
}
}
}
八,activity布局
<layout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<com.amap.api.maps2d.MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="200dp">
</com.amap.api.maps2d.MapView>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</layout>
九,適配器布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:padding="5dp"
android:id="@+id/title"
android:text="松日鼎盛"
android:textSize="20sp"
android:textColor="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:padding="5dp"
android:id="@+id/address"
android:textSize="16sp"
android:textColor="#666666"
android:text="深南大道"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>