• 【Android】18.2 利用百度定位服务API实现位置跟踪


    分类:C#、Android、VS2015;

    创建日期:2016-03-04

    一、简介

    第3章已经介绍过百度定位SDK,这里再演示一遍其基本用法。

    二、示例2—百度定位服务基本用法

    运行截图

    image

    设计步骤

    1、添加ch1802Main.xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
       <TextView
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/location_info"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />
        <com.baidu.mapapi.map.TextureMapView
            android:id="@+id/bmapView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true" />
    </LinearLayout>

    2、添加ch1802MainActivity.cs文件

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using Android.App;
    using Android.OS;
    using Android.Widget;
    using Android.Content.PM;
    using Com.Baidu.Location;
    using Com.Baidu.Mapapi.Map;
    using Com.Baidu.Mapapi.Model;
    
    namespace MyDemos.SrcDemos
    {
        [Activity(Label = "【例18-2】百度定位服务基本用法",
            ScreenOrientation = ScreenOrientation.Sensor,
            ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.KeyboardHidden)]
        public class ch1802MainActivity : Activity, IBDLocationListener
        {
            TextView locationInfo;
            LocationClient mLocationClient;
            TextureMapView mMapView;
            BaiduMap mBaiduMap;
            bool isFirstLoc = true;// 是否首次定位
    
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                SetContentView(Resource.Layout.ch1802Main);
                locationInfo = FindViewById<TextView>(Resource.Id.location_info);
                // 地图初始化
                mMapView = FindViewById<TextureMapView>(Resource.Id.bmapView);
                mBaiduMap = mMapView.Map;
                // 开启定位图层
                mBaiduMap.MyLocationEnabled = true;
                // 定位初始化
                mLocationClient = new LocationClient(this);
                mLocationClient.RegisterLocationListener(this);
                InitLocation();
                mLocationClient.Start();
    
                MyLocationConfiguration.LocationMode mCurrentMode = MyLocationConfiguration.LocationMode.Compass;
                BitmapDescriptor mCurrentMarker = null;
                mBaiduMap.SetMyLocationConfigeration(
                    new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker));
            }
            private void InitLocation()
            {
                LocationClientOption option = new LocationClientOption();
                //可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
                option.IsIgnoreKillProcess = false;
                //可选,默认false,设置是否需要过滤gps仿真结果,默认需要
                option.EnableSimulateGps = true;
                mLocationClient.LocOption = option;
            }
    
            //实现接口
            public void OnReceiveLocation(BDLocation p0)
            {
                // map view 销毁后不再处理新接收的位置
                if (p0 == null || mMapView == null) return;
    
                #region 模拟位置
                //注意:在手机上运行应注释掉这段代码,否则定位就没有意义了
                p0 = new BDLocation();
                p0.LocationDescribe = "模拟的位置,目的是为了在模拟器中查看运行效果";
                p0.Radius = 50;
                p0.Latitude = MainActivity.MyLatLng.Latitude;
                p0.Longitude = MainActivity.MyLatLng.Longitude;
                #endregion 模拟位置
    
                MyLocationData locData = new MyLocationData.Builder()
                        .Accuracy(p0.Radius)
                        // 此处设置开发者获取到的方向信息,顺时针0-360
                        .Direction(100).Latitude(p0.Latitude)
                        .Longitude(p0.Longitude).Build();
                mBaiduMap.SetMyLocationData(locData);
                if (isFirstLoc)
                {
                    isFirstLoc = false;
                    LatLng ll = new LatLng(p0.Latitude, p0.Longitude);
                    MapStatusUpdate u = MapStatusUpdateFactory.NewLatLng(ll);
                    mBaiduMap.AnimateMapStatus(u);
                }
    
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("latitude : " + p0.Latitude);
                sb.AppendLine("lontitude : " + p0.Longitude);
                sb.AppendLine("radius : " + p0.Radius);
                switch (p0.LocType)
                {
                    case BDLocation.TypeGpsLocation:
                        // GPS定位结果
                        sb.AppendLine("speed : " + p0.Speed);// 单位:公里/每小时
                        sb.AppendLine("satellite : " + p0.SatelliteNumber);
                        sb.AppendLine("height : " + p0.Altitude);// 单位:米
                        sb.AppendLine("direction : " + p0.Direction);// 单位度
                        sb.AppendLine("addr : " + p0.AddrStr);
                        sb.AppendLine("describe : gps定位成功");
                        break;
                    case BDLocation.TypeNetWorkLocation:
                        // 网络定位结果
                        sb.AppendLine("网络定位结果(addr) : " + p0.AddrStr);
                        //运营商信息
                        sb.AppendLine("运营商信息(operationers) : " + p0.Operators);
                        sb.AppendLine("describe : 网络定位成功");
                        break;
                    case BDLocation.TypeOffLineLocation:
                        // 离线定位结果
                        sb.AppendLine("describe : 离线定位成功,离线定位结果也是有效的");
                        break;
                    case BDLocation.TypeServerError:
                        sb.AppendLine("describe : 服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因");
                        break;
                    case BDLocation.TypeNetWorkException:
                        sb.AppendLine("describe : 网络不同导致定位失败,请检查网络是否通畅");
                        break;
                    case BDLocation.TypeCriteriaException:
                        sb.AppendLine("describe : 无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
                        break;
                }
                sb.AppendLine("locationdescribe: " + p0.LocationDescribe);// 位置语义化信息
                locationInfo.Text = sb.ToString();
            }
    
            protected override void OnPause()
            {
                mMapView.OnPause();
                base.OnPause();
            }
    
            protected override void OnResume()
            {
                mMapView.OnResume();
                base.OnResume();
            }
    
            protected override void OnDestroy()
            {
                // 退出时销毁定位
                mLocationClient.Stop();
                // 关闭定位图层
                mBaiduMap.MyLocationEnabled = false;
                mMapView.OnDestroy();
                mMapView = null;
                base.OnDestroy();
            }
        }
    }
  • 相关阅读:
    linux服务器时间同步
    07_DICTIONARY_ACCESSIBILITY
    Oracle监听静态注册和动态注册
    Oracle安装基本步骤
    笔试面试(2)阿里巴巴2014秋季校园招聘-软件研发工程师笔试题详解
    Global and Local Coordinate Systems
    齐次坐标(Homogeneous Coordinates)
    朴素贝叶斯分类器的应用
    图像数据到网格数据-1——MarchingCubes算法
    PCL源码剖析之MarchingCubes算法
  • 原文地址:https://www.cnblogs.com/rainmj/p/5243249.html
Copyright © 2020-2023  润新知