【Android】18.2 利用百度定位服务API实现位置跟踪
分类:C#、Android、VS2015;
创建日期:2016-03-04
一、简介
第3章已经介绍过百度定位SDK,这里再演示一遍其基本用法。
二、示例2—百度定位服务基本用法
运行截图

设计步骤
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();
}
}
}
【Android】18.2 利用百度定位服务API实现位置跟踪的更多相关文章
- 定位服务API案例
定位服务API案例 要使用定位服务API,需要确保设备已经下载并安装了HMS Core服务组件,并将Location Kit的SDK集成到项目中. 指定应用权限 Android提供了两种位置权限: A ...
- 利用百度文字识别API识别图像中的文字
本文将会介绍如何使用百度AI开放平台中的文字识别服务来识别图片中的文字.百度AI开放平台的访问网址为:http://ai.baidu.com/ ,为了能够使用该平台提供的AI服务,你需要事先注册一 ...
- [android学习]__使用百度地图开放api编写地图定位app
前言 在前面我已经记录关于如何使用百度地图api,以及如何配置相关的androidstudio配置了,接下来将记录如何使用百度地图api开发简单的地图定位apk,我将决定不定期持续更新本篇笔记,在每个 ...
- 安卓开发 利用百度识图api进行物体识别
前文 之前的随笔中,已经通过相机或相册获取到了我们想要的图片,接下来进行识图api的配置工作.我使用的是百度的api,利用python获取信息,并在MainActivity中进行调用来输出信息. 一. ...
- android开发对应高德地图定位服务进度一
进行android的高德地图开发首先需要进入高德地图的控制台进行注册登录.之后创建新的应用并且绑定软件得到相应的key. 这里面需要找到自己软件对应的多个SHA1.这里有发布版和调试版,以及对应的软件 ...
- 安卓开发 利用百度识图api进行物体识别(java版)
之前的随笔中,已经实现了python版本调用api接口,之所以使用python是因为python比java要简洁. 但是我发现在使用过程中,chaquopy插件会弹出底部toast显示"un ...
- C#封装百度Web服务API处理包含(Geocoding API,坐标转换API)
1.创建基础参数类 public static class BaiduConstParams { public const string PlaceApIv2Search = "http:/ ...
- Android Studio中利用JavaDoc生成项目API文档
1. 在Android Studio中的菜单项中点击Generate JavaDoc
- Android list刷新后仍然定位到原来的位置,解决。
问题: 有一个list,点击item时会做一些事情,然后重新加载数据,此时希望点击重新刷新后item还在原来的位置,而不是跳转到开头. 实现如下: 1.listview添加监听setOnScrollL ...
随机推荐
- 读取Style符号库样式的方法
以前进行符化的时候一般都是自定义Symbol,或者使用SymbologyControl进行选择,由于实际需要,我们来读取一下样式管理器中的样式.在ArcMap中打开如下:style下有很多样式类,每个 ...
- 二叉查找树实现实例(C语言)
/* search_tree.h */ #ifndef _SEARCH_TREE_H #define _SEARCH_TREE_H struct tree_node; typedef struct t ...
- spring-tool-suite(STS) 创建 spring boot项目
1.创建一个Spring Starter Project工程(new --> Spring Starter Project) 2.选择自己需要的依赖,因为想要通过REST方式来验证是否成功创建, ...
- word2vec模型cbow与skip-gram的比较
cbow和skip-gram都是在word2vec中用于将文本进行向量表示的实现方法,具体的算法实现细节可以去看word2vec的原理介绍文章.我们这里大体讲下两者的区别,尤其注意在使用当中的不同特点 ...
- 解决 在POM配置Maven plugin提示错误“Plugin execution not covered by lifecycle configuration”
eclipse在其POM文件的一处提示出错如下: Plugin execution not covered by lifecycle configuration: org.apache.maven.p ...
- HTML5 Canvas画图与动画学习59例
HTML5 Canvas画图与动画学习59例 学习HTML5 动画,画图的好资料. HTML5 Canvas画图与动画学习59例
- 如何使用jmeter来实现更大批量的并发的解决方案
近期在用JMeter进行负载测试的 时候,发现使用单台机器模拟测试超过比如500个进程的并发就有些力不从心或者说不能如实的反应实际情况,在执行的过程中,JMeter自身会自动关闭, 要解决这个问题,则 ...
- 公钥私钥与SSL的握手协议(转)
一,公钥私钥1,公钥和私钥成对出现2,公开的密钥叫公钥,只有自己知道的叫私钥3,用公钥加密的数据只有对应的私钥可以解密4,用私钥加密的数据只有对应的公钥可以解密5,如果可以用公钥解密,则必然是对应的私 ...
- Oracle PUP(PRODUCT_USER_PROFILE)配置和使用
近期在翻Oracle SQLPLUS官方文档,在讲SQLPLUS Security章节介绍了PUP这个机制.借此.我来使用下面: PUP(PRODUCT_USER_PROFILE)介绍 PRODU ...
- cxf之org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'cxf' available
原因是.... 把cxf的配置文件spring-cxf-rest.xml配置结束后,没有import到spring.xml中...所以才加载不到bean.... 另附:异常org.springfram ...