百度地圖或者高德地圖 實(shí)現(xiàn) 與地圖相關(guān)的功能
以高德地圖為例纹烹,百度地圖API 基本類似,看后臺(tái)要求召边,經(jīng)緯度要一致铺呵,消除轉(zhuǎn)經(jīng)緯度的問題。隧熙。
效果圖
定位
定位
地圖移動(dòng)
轉(zhuǎn)換地圖中心點(diǎn)
對(duì)話框
對(duì)話框
導(dǎo)航
導(dǎo)航
高德地圖 api: http://lbs.amap.com
百度地圖API:http://lbsyun.baidu.com/index.php?title=androidsdk
定位輔助類
public class LocationProvider {
public AMapLocationClient getLocationClient() {
return locationClient;
}
public void setLocationClient(AMapLocationClient locationClient) {
this.locationClient = locationClient;
}
private AMapLocationClient locationClient = null;
// private LocationClientOption mOption,DIYoption;
private Object objLock = new Object();
private AMapLocationClientOption DIYoption;
/***
* @param locationContext
*/
public LocationProvider(Context locationContext) {
synchronized (objLock) {
if (locationClient == null) {
locationClient = new AMapLocationClient(locationContext);
locationClient.setLocationOption(getDefaultOption());
}
}
}
/**
* 默認(rèn)的定位參數(shù)
*
* @author hongming.wangLocationProvider
* @since 2.8.0
*/
public AMapLocationClientOption getDefaultOption() {
AMapLocationClientOption mOption = new AMapLocationClientOption();
mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可選片挂,設(shè)置定位模式,可選的模式有高精度、僅設(shè)備音念、僅網(wǎng)絡(luò)沪饺。默認(rèn)為高精度模式
mOption.setGpsFirst(true);//可選,設(shè)置是否gps優(yōu)先闷愤,只在高精度模式下有效整葡。默認(rèn)關(guān)閉
mOption.setHttpTimeOut(20000);//可選,設(shè)置網(wǎng)絡(luò)請(qǐng)求超時(shí)時(shí)間讥脐。默認(rèn)為30秒遭居。在僅設(shè)備模式下無效
mOption.setInterval(2000);//可選,設(shè)置定位間隔旬渠。默認(rèn)為2秒
mOption.setNeedAddress(true);//可選俱萍,設(shè)置是否返回逆地理地址信息。默認(rèn)是true
mOption.setOnceLocation(false);//可選告丢,設(shè)置是否單次定位枪蘑。默認(rèn)是false
mOption.setOnceLocationLatest(false);//可選,設(shè)置是否等待wifi刷新岖免,默認(rèn)為false.如果設(shè)置為true,會(huì)自動(dòng)變?yōu)閱未味ㄎ恍瓤埽掷m(xù)定位時(shí)不要使用
AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可選, 設(shè)置網(wǎng)絡(luò)請(qǐng)求的協(xié)議觅捆∩庖郏可選HTTP或者HTTPS。默認(rèn)為HTTP
mOption.setSensorEnable(false);//可選栅炒,設(shè)置是否使用傳感器掂摔。默認(rèn)是false
mOption.setWifiScan(true); //可選,設(shè)置是否開啟wifi掃描赢赊。默認(rèn)為true乙漓,如果設(shè)置為false會(huì)同時(shí)停止主動(dòng)刷新,停止以后完全依賴于系統(tǒng)刷新释移,定位位置可能存在誤差
mOption.setLocationCacheEnable(true); //可選叭披,設(shè)置是否使用緩存定位,默認(rèn)為true
return mOption;
}
/**
* 銷毀定位
*
* @author hongming.wang
* @since 2.8.0
*/
public void destroyLocation() {
if (null != locationClient) {
/**
* 如果AMapLocationClient是在當(dāng)前Activity實(shí)例化的玩讳,
* 在Activity的onDestroy中一定要執(zhí)行AMapLocationClient的onDestroy
*/
locationClient.onDestroy();
locationClient = null;
}
}
public boolean registerListener(AMapLocationListener locationListener) {
boolean isSuccess = false;
if (locationListener != null) {
locationClient.setLocationListener(locationListener);
isSuccess = true;
}
return isSuccess;
}
public void unregisterListener(AMapLocationListener locationListener) {
if (locationListener != null) {
locationClient.unRegisterLocationListener(locationListener);
}
}
/***
* @param option
* @return isSuccessSetOption
*/
public boolean setLocationOption(AMapLocationClientOption option) {
boolean isSuccess = false;
if (option != null) {
if (locationClient.isStarted())
locationClient.stopLocation();
DIYoption = option;
locationClient.setLocationOption(option);
isSuccess = true;
}
return isSuccess;
}
/**
* 開始定位
*
* @author hongming.wang
* @since 2.8.0
*/
public void start() {
synchronized (objLock) {
if (locationClient != null && !locationClient.isStarted()) {
locationClient.startLocation();
}
}
}
/**
* 停止定位
*
* @author hongming.wang
* @since 2.8.0
*/
public void stop() {
synchronized (objLock) {
if (locationClient != null && locationClient.isStarted()) {
locationClient.stopLocation();
}
}
}
}
地圖移動(dòng) 改變中心點(diǎn)
aMap.animateCamera(CameraUpdateFactory.newLatLngZoom((LatLng) hotAreaList.get(position), 14));
addMarkersToMap((LatLng) hotAreaList.get(position), position + 1);
顯示自定義對(duì)話框
在界面中做好自己的View 然后顯示隱藏就可以了涩蜘。
/**
* 根據(jù)info為布局上的控件設(shè)置信息
*
* @param
* @param
*/
protected void popupInfo(ViewGroup mMarkerLy, int
index) {
mMarkerLy.setVisibility(View.VISIBLE);
TextView parkAddressText = (TextView) mMarkerLy
.findViewById(R.id.parkAddressText);
TextView canUseParkNumberText = (TextView) mMarkerLy
.findViewById(R.id.canUseParkNumberText);
TextView distanceText = (TextView) mMarkerLy
.findViewById(R.id.distanceText);
TextView supportOnlineText = (TextView) mMarkerLy
.findViewById(R.id.supportOnlineText);
TextView perForTimeText = (TextView) mMarkerLy
.findViewById(R.id.perForTimeText);
Button goHereButton = (Button) mMarkerLy
.findViewById(R.id.goHereButton);
LinearLayout infoLayout = (LinearLayout) mMarkerLy.findViewById(R.id.parkDetialLineraLayout);
// final int finalIndex = index;
// final ParkInfo info = nearList.get(finalIndex);
parkAddressText.setText("停車場(chǎng)地址");
perForTimeText.setText("停車場(chǎng)距離");
//
// DecimalFormat decimalFormat = new DecimalFormat("0.0");//構(gòu)造方法的字符格式這里如果小數(shù)不足2位,會(huì)以0補(bǔ)足.
// String p = decimalFormat.format(distance / 1000);//format 返回的是字符串
distanceText.setText("3KM");
canUseParkNumberText.setText("空車位數(shù)量");
supportOnlineText.setText("支持線上支付");
supportOnlineText.setVisibility(View.VISIBLE);
goHereButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MapActivity.this, AMapNavBaseActivity.class);
NaviLatLng endlatlng = new NaviLatLng(32.0414637353, 118.7851165312);
NaviLatLng beginLatlng = new NaviLatLng(loctionLatLng.latitude,
loctionLatLng.longitude);
Bundle bundle = new Bundle();
bundle.putParcelable("beginLatlng", beginLatlng);
bundle.putParcelable("endlatlng", endlatlng);
intent.putExtras(bundle);
startActivity(intent);
// AbToastUtil.showToast(MapActivity.this, "導(dǎo)航");
}
});
infoLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//
}
});
}
導(dǎo)航 根據(jù)官方Demo 編寫調(diào)試
高德地圖 需要自己申請(qǐng)科大訊飛 Key 做替換
高德地圖 語音提示是 使用的第三方的語音庫,在搭環(huán)境的時(shí)候要注意熏纯。
代碼不定時(shí)更新 https://github.com/chinaltz/JustAndroid