1. 程式人生 > >呼叫百度地圖SDK顯示當前定位位置

呼叫百度地圖SDK顯示當前定位位置

根據經緯度顯示位置和地址

public class LocationFragment extends BaseFragment implements View.OnClickListener, OnGetGeoCoderResultListener {

    @Override
    public View onCreateViewIfNull(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Intent intent = this.getActivityIntent();
        if (intent != null) {
            Bundle bundle = intent.getBundleExtra(KeyContants.ACTIVITYBUNDLE);
            if (bundle != null) {
                double latitude = bundle.getDouble(KeyContants.LATITUDE);
                double longitude = bundle.getDouble(KeyContants.LONGITUDE);
                if (latitude > 0 && longitude > 0) {
                    shopPoint = new LatLng(latitude, longitude);
                }
                shopAddress =bundle.getString(KeyContants.ADDRESSS);
            }
        }

        mParentView = inflater.inflate(R.layout.zeno_frag_location, null);
        ViewUtils.inject(this, mParentView);
        initView();
        initData();
        initListener();
        return mParentView;
    }

    @Override
    public TitleBarInfo onCreateTitleBar() {
        TitleBarInfo info = Factory.getInstance().getTitleResManager().createStandardWithBack(getActivity());
        info.setTitle("店鋪位置");
        return info;
    }

    private void initView() {
        tvShopAddress = (TextView) View.inflate(getActivity(), R.layout.zeno_layout_address_marker, null);

        BaiduMapOptions mapOptions = new BaiduMapOptions().compassEnabled(false).zoomControlsEnabled(true).rotateGesturesEnabled(false);
        mMapView = new MapView(getActivity(), mapOptions);
        mRLMap.addView(mMapView, 0);

        // 獲取當前定位城市
        GeoPoint gp = Factory.getInstance().getGeoMap().getGP();
        myPoint = new LatLng(gp.getLatitude(), gp.getLongitude());

        mGeoCoder = GeoCoder.newInstance();
        mGeoCoder.setOnGetGeoCodeResultListener(this);

        mBaiduMap = mMapView.getMap();

        mBaiduMap.setOnMapLoadedCallback(new BaiduMap.OnMapLoadedCallback() {
            @Override
            public void onMapLoaded() {
                //地圖載入完成後再設定Bound
                hasLoaded=true;
                Logger.e("leon","onMapLoaded");
                //調整縮放比例,將定位和店鋪座標同時顯示
                LatLngBounds.Builder builder = new LatLngBounds.Builder();
                if(myPoint!=null && shopPoint!=null){
                    Logger.e("leon","onMapLoaded mypoint:"+myPoint.latitude+":"+myPoint.longitude+"   shop point:"+shopPoint.latitude+":"+shopPoint.longitude);
                    builder.include(myPoint).include(shopPoint);
                    mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLngBounds(builder.build()));
                    Logger.e("leon","onMapLoaded zoom:"+mBaiduMap.getMapStatus().zoom);
                }

            }
        });

        // // 設定縮放級別
        MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(17.5f);
        mBaiduMap.setMapStatus(msu);

        //顯示定點陣圖標
        // 修改為自定義marker
        mCurrentMarker = BitmapDescriptorFactory
                .fromResource(R.drawable.zeno_ic_map_center_location);
        mBaiduMap
                .setMyLocationConfigeration(new MyLocationConfiguration(
                        mCurrentMode, true, mCurrentMarker));

        // 開啟定點陣圖層
        mBaiduMap.setMyLocationEnabled(true);
        if (myPoint != null && myPoint.latitude > 0 && myPoint.longitude > 0) {
            setMyLocation(myPoint);
        } else {//重新獲取定位座標
            mLocClient = new LocationClient(getActivity());
            mLocClient.registerLocationListener(myListener);
            LocationClientOption option = new LocationClientOption();
            option.setOpenGps(true); // 開啟gps
            option.setCoorType("bd09ll"); // 設定座標型別
            option.setScanSpan(5000);
            mLocClient.setLocOption(option);
            mLocClient.start();
        }

        LatLng point = DEFAULT_LOCATION;
        if (shopPoint != null && shopPoint.latitude > 0 && shopPoint.longitude > 0) {
            point = shopPoint;
            //有店鋪經緯度,則去取詳細地址,並顯示在地圖上
            mGeoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(shopPoint));
        } else {
            MsgToast.geToast().setLongMsg("該店鋪尚未設定地圖位置");
            if (myPoint != null && myPoint.latitude > 0 && myPoint.longitude > 0) {
                //沒有店鋪經緯度,顯示當前定位
                point = myPoint;
            }
            moveTo(point);
        }
    }

    private void setMyLocation(LatLng point) {
        MyLocationData locData = new MyLocationData.Builder()
                .latitude(point.latitude)
                .longitude(point.longitude).build();
        mBaiduMap.setMyLocationData(locData);
    }

    private void initData() {

    }

    private void initListener() {
        mIVMyLocation.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (mIVMyLocation == v) {
            //跳轉到定位
            if (myPoint != null) {
                moveTo(myPoint);
            } else {
                if (mLocClient != null) {
                    mLocClient.start();
                }
            }
        }
    }

    private void moveTo(LatLng point) {
        MapStatus.Builder builder = new MapStatus.Builder();
        builder.target(point);
        mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
    }

    @Override
    public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {

    }

    @Override
    public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
        if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
            // 沒有找到
            moveTo(myPoint);
            return;
        }

        try {
            //找到了則移動到店鋪地址
            String address = result.getAddress();
            //在地圖上新增圖示
            tvShopAddress.setText(!TextUtils.isEmpty(shopAddress)?shopAddress:address);
            MarkerOptions shopMarker = new MarkerOptions().position(result.getLocation()).icon(BitmapDescriptorFactory.fromView(tvShopAddress));
            mBaiduMap.addOverlay(shopMarker);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 定位SDK監聽函式
     */
    public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {

            // map view 銷燬後不在處理新接收的位置
            if (location == null || mMapView == null) {
                return;
            }
            myPoint = new LatLng(location.getLatitude(), location.getLongitude());
            setMyLocation(myPoint);

            //定位成功,停止定位
            if(mLocClient != null){
                mLocClient.stop();
            }
        }

        public void onReceivePoi(BDLocation poiLocation) {
        }
    }

    @Override
    public void onPause() {
        // MapView的生命週期與Activity同步,當activity掛起時需呼叫MapView.onPause()
        if (mMapView != null) {
            mMapView.onPause();
        }
        super.onPause();
    }

    @Override
    public void onResume() {
        // MapView的生命週期與Activity同步,當activity恢復時需呼叫MapView.onResume()
        if (mMapView != null) {
            mMapView.onResume();
        }
        super.onResume();
    }

    @Override
    public void onDestroy() {
        try {
            if (mGeoCoder != null) {
                mGeoCoder.destroy();
                mGeoCoder = null;
            }

            // 退出時銷燬定位
            if(mLocClient!=null){
                mLocClient.stop();
            }

            // MapView的生命週期與Activity同步,當activity銷燬時需呼叫MapView.destroy()
            if (mMapView != null) {
                mMapView.onDestroy();
                mMapView = null;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }

    /**
     * 預設位置,公司座標
     */
    private static final LatLng DEFAULT_LOCATION = new LatLng(22.518143, 113.930614);


    private View mParentView;

    private MapView mMapView = null;
    @ViewInject(R.id.zeno_mRLMap)
    private RelativeLayout mRLMap;

    private BaiduMap mBaiduMap = null;
    private LatLng myPoint;//當前定位
    private LatLng shopPoint;//店鋪位置
    private String shopAddress;
    private TextView tvShopAddress;

    /**
     * 根據地理座標查詢地址
     */
    private GeoCoder mGeoCoder = null;

    // 定位相關
    @ViewInject(R.id.mIVMyLocation)
    private ImageView mIVMyLocation;

    private LocationClient mLocClient;
    public MyLocationListenner myListener = new MyLocationListenner();
    private MyLocationConfiguration.LocationMode mCurrentMode;
    private BitmapDescriptor mCurrentMarker;

    private boolean hasLoaded=false;
}

相關推薦

呼叫地圖SDK顯示當前定位位置

根據經緯度顯示位置和地址 public class LocationFragment extends BaseFragment implements View.OnClickListener, OnGetGeoCoderResultListener { @Ove

H5通過地圖API獲取當前地理位置

首先去百度地圖開飯平臺註冊key 博主親測註冊十分方便,不到3分鐘就完成了。 完整程式碼如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script t

使用地圖SDK定位當前位置顯示地圖

package com.shz.baidumap; import android.Manifest; import android.content.pm.PackageManager; import android.os.Bundle; import andr

Android 地圖開發(一)如何呼叫地圖介面和在專案中顯示地圖以及實現定位

二、下載百度地圖API庫 然後新增到專案中即可。   三、在專案清單AndroidMainifest.xml配置百度地圖API key和新增相關許可權                         四、在專案呼叫百度地圖專案功能,這篇文章就首先講講顯示地圖和定位的功能 首先

js 呼叫地圖,並且定位使用者地址,顯示省市區街,經緯度

<!DOCTYPE html> <html> <head>     <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />     <

iOS·採用第三方(地圖SDK)實現定位等功能開發

 陳滿iOS  關注 2017.05.01 01:06* 字數 2212 閱讀 6818評論 7喜歡 133 1.申請金鑰 首先,申請一個baidu賬號,接著進入新建金鑰入口申請成為baidu地圖開發者,填寫相關開發者資訊和簡訊驗證碼。接

呼叫地圖API定位位置

以下程式碼是百度開發示例程式碼: <html> <head>     <meta http-equiv="Content-Type" content="text/html; charse

地圖SDK整合定位,衛星地圖

其中兩種整合方式基本相同 1.百度地圖第三方SDK的開放平臺建立應用 2.下載官方的SDK 3.複製到專案的libs目錄下 4.但這樣工程並不會自動載入libs下的so檔案,需配置so檔案的路徑為該libs路徑,關聯所有地圖SDK的so檔案 App下的groud.build的

mui使用地圖sdk定位方法

一、使用前配置:參考http://ask.dcloud.net.cn/article/29        注意事項:     (1)HBuilder預設打包使用的簽名為"BA:AD:09:3A:82:82:9F:

js呼叫地圖api實現定位

<?php /** * Created by PhpStorm. * User: onlythen * Date: 5/26/15 * Time: 3:23 PM */ session_start(); require_once("config.php"); $link_id=mys

地圖SDK(二)——簡單地圖定位demo

MainActivity程式碼如下:package com.example.administrator; import android.content.Intent; import android.os.Bundle; import android.support.v7.ap

呼叫地圖api,通過ip獲取當前城市以及經緯度

一、申請Ak值 登入百度地圖開放平臺: 建立應用: 建立成功後ip值就有了: 二、程式碼實現 <?php $ip="xxxxxxx"; $content=file_get_contents("http://api.map.baidu.com/location/ip?

vue移動端app呼叫地圖實現簡單定位

最近做畢業設計做了一個手機定位的小功能,在此記錄下來,我這個定在在電腦上進行測試是沒有許可權的,只有在手機上測試才能拿到具體的地理位置。第一步先是建立一個map.js檔案,用來引入和暴露百度地圖的元件,具體內容如下:export function MP(ak) { retur

第三方SDK地圖(二)定位 + 鷹眼軌跡

#1 基礎地圖 + 基礎定位# 可以看到地圖的介面。 如圖: Menu: <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app

地圖SDK V3.2 和定位SDK V4.2 完成定位功能

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.example.getsmsinfo"  

android使用地圖SDK獲取定位資訊

本文使用Android Studio開發。 獲取定位資訊相對簡單,我們只需要如下幾步: 第三步,建立Android Studio工程(略過不說),配置環境: 將解壓後的檔案放入libs資料夾下,並在src/main下建立一個叫做jniLibs的資料

安卓開發使用地圖sdk實現定位,新增marker,城市切換等功能

許久不寫部落格,慢慢的變的有些懶惰。keep coding,keep learining。 關於學習使用百度地圖sdk,我六點要說。。。。。。哈哈哈哈,玩笑歸玩笑,說正經的,百度地圖sdk的幫助文件寫的不是很好,很多地方不完善,有偷懶的嫌疑,建議學習時配合類參考,有歧義不清

呼叫地圖進行定位的Demo

<!DOCTYPE html> <html> <head> <meta name="viewport" content="initial-sca

Android使用地圖SDK實現定位與方向感測器匹配

public class MylocationListener implements BDLocationListener { //定位請求回撥介面 private boolean isFirstIn=true; //定位請求回撥函式,這裡面會得到定位資

Android 地圖sdk 標註圖marker中可以切換顯示不同內容

記錄一個前段時間解決的功能需求 先直接上圖片看看實現後的效果: 具體需求為,在地圖頁上顯示出所有的場站marker之後,點選左側的按鈕可以實現動態切換場站marker中顯示的資料。 實現思路為:構造marker時,icon方法中傳入的引數Bit