admin 管理员组文章数量: 888329
Android 仿微信发送坐标,Android最新版高德地圖poi檢索仿微信發送位置
好久一段時間沒有寫博客了,陸續會補上最近項目中用到的。由於項目需要一個定位的功能,參考下市面上現有的地圖SDK,覺得高德地圖不錯,本項目中用的是高德地圖定位。首先演示下效果:
需求分析:
從gif當中,我們看到,大概可以分為這么幾個行為:
1. 進入頁面,產生兩個標記點,周圍地點列表。
兩個標記點:一個是 固定不變 的 當前位置的定位圓形標記;一個是 可移動 的 紅色標記,並且,會根據當前經緯度改變位置
地址列表:就是根據當前剛進入頁面的經緯度搜索的出來的 附近poi (附近興趣點)
2. 當我們拖動地圖的時候,地圖中心的mark不變,但會不斷查詢新的地理位置,也就是動態的改變位置。
可移動的地圖上的 紅色標記 處於地圖的 中心點位置
3. 當我們點擊搜索框進行搜索時,會自動補全信息。點擊對應的item,地圖會定位到對應的位置上。
導入地圖:
導入高德地圖,由於這里使用的是最新版本,可以通過Gradle集成SDK,可以根據需求導入對應的sdk:
3D地圖compile 'com.amap.api:3dmap:latest.integration'
2D地圖compile 'com.amap.api:map2d:latest.integration'
導航compile 'com.amap.api:navi-3dmap:latest.integration'
搜索compile 'com.amap.api:search:latest.integration'
定位compile 'com.amap.api:location:latest.integration'
然后:
android { defaultConfig { ndk { //設置支持的SO庫架構(開發者可以根據需要,選擇一個或多個平台的so) abiFilters "armeabi", "armeabi-v7a", "arm64-v8a", "x86","arm64-v8a","x86_64" } } },如果需要查看更多相關資料可以前往開放平台進行查看(點這里)。
顯示我的位置:
當然Android6.0之后需要申請動態權限,這里就先不說如何申請了,比較簡單,就是幾行代碼的事。這里設置我的位置相關屬性(實現定位藍點(5.0.0版本后)):
aMap = mapView.getMap();
MyLocationStyle myLocationStyle;
myLocationStyle = new MyLocationStyle();//初始化定位藍點樣式類myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//連續定位、且將視角移動到地圖中心點,定位點依照設備方向旋轉,並且會跟隨設備移動。(1秒1次定位)如果不設置myLocationType,默認也會執行此種模式。
myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.mipmap.my_location));
myLocationStyle.radiusFillColor(android.R.color.transparent);
myLocationStyle.strokeColor(android.R.color.transparent);
myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE);//定位一次,且將視角移動到地圖中心點。
myLocationStyle.showMyLocation(true);
myLocationStyle.interval(2000); //設置連續定位模式下的定位間隔,只在連續定位模式下生效,單次定位模式下不會生效。單位為毫秒。
aMap.setMyLocationStyle(myLocationStyle);//設置定位藍點的Style
aMap.getUiSettings().setMyLocationButtonEnabled(true);
aMap.setMyLocationEnabled(true);// 設置為true表示啟動顯示定位藍點,false表示隱藏定位藍點並不進行定位,默認是false。
aMap.moveCamera(CameraUpdateFactory.zoomTo(17f));
aMap.setOnCameraChangeListener(this);//手動移動地圖監聽 (2)
aMap.getUiSettings().setMyLocationButtonEnabled(true);// 設置默認定位按鈕是否顯示
//設置為true表示顯示定位層並可觸發定位,false表示隱藏定位層並不可觸發定位,默認是false
aMap.setMyLocationEnabled(true);
(這里說明一下,5.0之前需要,在aMap.setLocationSource(this)中包含兩個回調,activate(OnLocationChangedListener)和deactivate()。在activate()中設置定位初始化及啟動定位,在deactivate()中寫停止定位的相關調用。)
設置marker:
mMarkerOptions = new MarkerOptions();
mMarkerOptions.draggable(false);//可拖放性
mMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.mark));
mCenterMarker = aMap.addMarker(mMarkerOptions);
ViewTreeObserver vto = mapView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mCenterMarker.setPositionByPixels(mapView.getWidth() >> 1, mapView.getHeight() >> 1);
mCenterMarker.showInfoWindow();
}
});
(將marker一直設置在中心位置)
逆地理編碼,經緯度轉地址:
逆地理編碼 大概是這么幾步走的:
1、得到 GeocodeSearch 的實例
2、實現 GeocodeSearch.OnGeocodeSearchListener 接口
3、實現接口就必須實現實現一下兩方法 onRegeocodeSearched 和 onGeocodeSearched。
Geocode 是地理編碼的意思,Regeocode 就是逆地理編碼的。
所以我們主要逆地理實現邏輯都在 onRegeocodeSearched 方法:
@Override
public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
if (rCode == 1000) {
if (result != null && result.getRegeocodeAddress() != null &&
result.getRegeocodeAddress().getFormatAddress() != null) {
currentPosition = result.getRegeocodeAddress().getFormatAddress();
mPoiQuery = new PoiSearch.Query("", "", result.getRegeocodeAddress().getCityCode());
mPoiQuery.setPageSize(10);// 設置每頁最多返回多少條poiitem
mPoiQuery.setPageNum(0);//設置查第一頁
PoiSearch poiSearch = new PoiSearch(this, mPoiQuery);
poiSearch.setOnPoiSearchListener(this);//設置數據返回的監聽器 (5)
//設置周邊搜索的中心點以及區域
poiSearch.setBound(new PoiSearch.SearchBound(mCurrentPoint, 1000, true));
poiSearch.searchPOIAsyn();//開始搜索
Log.i(TAG, "onRegeocodeSearched: " + currentPosition);
} else {
Toast.makeText(this, "無結果", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "" + rCode, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
if (rCode == 1000) {
if (result != null && result.getRegeocodeAddress() != null &&
result.getRegeocodeAddress().getFormatAddress() != null) {
currentPosition = result.getRegeocodeAddress().getFormatAddress();
mPoiQuery = new PoiSearch.Query("", "", result.getRegeocodeAddress().getCityCode());
mPoiQuery.setPageSize(10);// 設置每頁最多返回多少條poiitem
mPoiQuery.setPageNum(0);//設置查第一頁
PoiSearch poiSearch = new PoiSearch(this, mPoiQuery);
poiSearch.setOnPoiSearchListener(this);//設置數據返回的監聽器 (5)
//設置周邊搜索的中心點以及區域
poiSearch.setBound(new PoiSearch.SearchBound(mCurrentPoint, 1000, true));
poiSearch.searchPOIAsyn();//開始搜索
Log.i(TAG, "onRegeocodeSearched: " + currentPosition);
} else {
Toast.makeText(this, "無結果", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "" + rCode, Toast.LENGTH_SHORT).show();
}
}
(在這里我們再根據經緯度去查詢當前的興趣點)
根據經緯度搜索周圍poi信息,在回調方法中進行操作:
@Override
public void onPoiSearched(PoiResult result, int rcode) {
if (rcode == 1000) {
if (result != null && result.getQuery() != null) {// 搜索poi的結果
if (result.getQuery().equals(mPoiQuery)) {// 是否是同一條
poiItems = result.getPois();
// 當搜索不到poiitem數據時,會返回含有搜索關鍵字的城市信息
suggestionCities = result
.getSearchSuggestionCitys();
Log.i(TAG, "onPoiSearched: " + poiItems);
list.clear();
list.add(new Location("我的位置", currentPosition));
for (PoiItem p : poiItems) {
list.add(new Location(p.getTitle(), p.getSnippet(), p.getLatLonPoint().getLatitude(), p.getLatLonPoint().getLongitude()));
}
adapter.notifyDataSetChanged();
/**
* listviw具體操作邏輯
*/
}
} else if (suggestionCities != null
&& suggestionCities.size() > 0) {
showSuggestCity(suggestionCities);
} else {
Toast.makeText(this, "對不起,沒有搜索到相關數據!", Toast.LENGTH_SHORT).show();
}
}
}
(並且,將其添加到listview中。)
最后,說一下輸入內容自動提示:
其實也就是幾個回調函數,監聽文本的輸入,在回調函數中添加對應的操作:
@Override
public void onGetInputtips(List list, int i) {
autoList.clear();
for (Tip p : list) {
autoList.add(p);
}
adapter1.notifyDataSetChanged();
}
最后有一點要點,5.0之后可能會出現,高德地圖黑屏或者白屏問題,經過測試重寫下以下方法即可:
@Override
public Resources getResources() {
return getBaseContext().getResources();
}
本文标签: Android 仿微信发送坐标 Android最新版高德地圖poi檢索仿微信發送位置
版权声明:本文标题:Android 仿微信发送坐标,Android最新版高德地圖poi檢索仿微信發送位置 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1688275632h200354.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论