Android從0到完整項(xiàng)目(7)地圖與導(dǎo)航

百度地圖或者高德地圖 實(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末同诫,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子樟澜,更是在濱河造成了極大的恐慌误窖,老刑警劉巖叮盘,帶你破解...
    沈念sama閱讀 218,386評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異霹俺,居然都是意外死亡柔吼,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門丙唧,熙熙樓的掌柜王于貴愁眉苦臉地迎上來嚷堡,“玉大人,你說我怎么就攤上這事艇棕◎蚪洌” “怎么了?”我有些...
    開封第一講書人閱讀 164,704評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵沼琉,是天一觀的道長(zhǎng)北苟。 經(jīng)常有香客問我,道長(zhǎng)打瘪,這世上最難降的妖魔是什么友鼻? 我笑而不...
    開封第一講書人閱讀 58,702評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮闺骚,結(jié)果婚禮上彩扔,老公的妹妹穿的比我還像新娘。我一直安慰自己僻爽,他們只是感情好虫碉,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,716評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著胸梆,像睡著了一般敦捧。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上碰镜,一...
    開封第一講書人閱讀 51,573評(píng)論 1 305
  • 那天兢卵,我揣著相機(jī)與錄音,去河邊找鬼绪颖。 笑死秽荤,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的柠横。 我是一名探鬼主播窃款,決...
    沈念sama閱讀 40,314評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼滓鸠!你這毒婦竟也來了雁乡?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,230評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤糜俗,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體悠抹,經(jīng)...
    沈念sama閱讀 45,680評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡珠月,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,873評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了楔敌。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片啤挎。...
    茶點(diǎn)故事閱讀 39,991評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖卵凑,靈堂內(nèi)的尸體忽然破棺而出庆聘,到底是詐尸還是另有隱情,我是刑警寧澤勺卢,帶...
    沈念sama閱讀 35,706評(píng)論 5 346
  • 正文 年R本政府宣布伙判,位于F島的核電站,受9級(jí)特大地震影響黑忱,放射性物質(zhì)發(fā)生泄漏宴抚。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,329評(píng)論 3 330
  • 文/蒙蒙 一甫煞、第九天 我趴在偏房一處隱蔽的房頂上張望菇曲。 院中可真熱鬧,春花似錦抚吠、人聲如沸常潮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蕊玷。三九已至,卻和暖如春弥雹,著一層夾襖步出監(jiān)牢的瞬間垃帅,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評(píng)論 1 270
  • 我被黑心中介騙來泰國打工剪勿, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留贸诚,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,158評(píng)論 3 370
  • 正文 我出身青樓厕吉,卻偏偏與公主長(zhǎng)得像酱固,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子头朱,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,941評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容