Android获取当前位置,推荐使用百度地图SDK获取位置
这里直接引用了百度V2.6出给出的DEMO实例,经过测试,在手机客户端可以获取到当前所在的位置 首先先看一下MainActivity.java
package com.example.locationmanager;import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption;import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView;public class MainActivity extends Activity {private LocationClient mLocationClient = null; private Button mStartBtn = null; private TextView mTextView = null;@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mStartBtn = (Button)findViewById(R.id.button1); mTextView = (TextView)findViewById(R.id.textview1);mLocationClient = new LocationClient(this); LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); //打开gps option.setCoorType("bd09ll"); //设置坐标类型为bd09ll option.setPriority(LocationClientOption.NetWorkFirst); //设置网络优先 option.setProdName("locSDKDemo2"); //设置产品线名称 option.setScanSpan(5000); //定时定位,每隔5秒钟定位一次。 mLocationClient.setLocOption(option); mLocationClient.registerLocationListener(new BDLocationListener() { @Override public void onReceiveLocation(BDLocation location) { if (location == null) return ; StringBuffer sb = new StringBuffer(256); sb.append("time : "); sb.append(location.getTime()); sb.append("\nerror code : "); sb.append(location.getLocType()); sb.append("\nlatitude : "); sb.append(location.getLatitude()); sb.append("\nlontitude : "); sb.append(location.getLongitude()); sb.append("\nradius : "); sb.append(location.getRadius()); if (location.getLocType() == BDLocation.TypeGpsLocation){ sb.append("\nspeed : "); sb.append(location.getSpeed()); sb.append("\nsatellite : "); sb.append(location.getSatelliteNumber()); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){ sb.append("\naddr : "); sb.append(location.getAddrStr()); } sb.append("\nsdk version : "); sb.append(mLocationClient.getVersion()); mTextView.setText(sb.toString()); }public void onReceivePoi(BDLocation location){ //return ; } });mStartBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mLocationClient == null) return ; if (mLocationClient.isStarted()){ mLocationClient.stop(); mStartBtn.setText("开始"); } else { mLocationClient.start(); mStartBtn.setText("关闭"); } } });}@Override public void onDestroy(){ if (mLocationClient != null && mLocationClient.isStarted()){ mLocationClient.stop(); mLocationClient = null; } super.onDestroy(); } }
接下来看一下 activity_main.xml
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" ><TextView android:id="@+id/textview1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="hello" /><RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="match_parent" android:layout_height="match_parent" ><Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="开始" /> </RelativeLayout></LinearLayout>
最后根据百度文档提出的权限问题,我们加到 AndroidMainFest.xml 中
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.locationmanager" android:versionCode="1" android:versionName="1.0" ><uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /><permission android:name="android.permission.BAIDU_LOCATION_SERVICE" > </permission><uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" > </uses-permission> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" > </uses-permission> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" > </uses-permission> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" > </uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" > </uses-permission> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" > </uses-permission> <uses-permission android:name="android.permission.READ_PHONE_STATE" > </uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" > </uses-permission> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" > </uses-permission> <uses-permission android:name="android.permission.READ_LOGS" > </uses-permission><application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.locationmanager.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity><service android:name="com.baidu.location.f" android:enabled="true" android:permission="android.permission.BAIDU_LOCATION_SERVICE" android:process=":remote" > <intent-filter> <action android:name="com.baidu.location.service_v2.6" > </action> </intent-filter> </service> </application></manifest>
运行后,就可以得到相应的位置信息了。同时我们还可以使用百度提供的其它服务,如地图标注功能等。
Android获取当前位置,推荐使用百度地图SDK获取位置相关推荐
- H5使用百度地图SDK获取用户当前位置并且标记显示在地图
代码实现功能: H5使用百度地图SDK获取用户当前位置并且标记显示在地图,点击该标记弹出一层自定义的HTML. 效果图: 代码: <!DOCTYPE html> <html>& ...
- android 记录路线轨迹_基于百度地图SDK记录运动轨迹
标签 : 运动轨迹 百度地图SDK 最终实现的效果如下图所示### 注意1:模拟器上模拟要设置好Location(如果选择None就无法定位了),同时第一次打开应用会提示授权使用地理位置信息,点击允许 ...
- android studio百度地图SDK获取当前位置和经纬度和海拔
网上很多获取位置的博文,但是感觉都不是很全面. 最近刚好有一个项目要求获取当地的经纬度和地理位置 几经折腾,终于还是写出来了.文分享一下. 代码如下 先在AndroidManifest中添加如下权限 ...
- Android百度地图海拔,android studio百度地图SDK获取当前位置和经纬度和海拔
网上很多获取位置的博文,但是感觉都不是很全面. 最近刚好有一个项目要求获取当地的经纬度和地理位置 几经折腾,终于还是写出来了.文分享一下. 代码如下 先在AndroidManifest中添加如下权限 ...
- html5 直接获取当前位置,HTML5调用百度地图API获取当前位置并直接导航目的地的方法...
//$ak=2051409c59b8d558c7e6bbd0c5c62ffd body, html,#allmap {width: 100%;height: 100%;overflow: hidden ...
- android的百度地图sdk获取ip,基于百度地图API的ip地址查询
说明:使用ip地址查询太麻烦,偶然搜索发现有人已经写过基于百度API的ip地址查询,这个是前人的结晶,我只是修改了一些,勿喷. 用的python2.7,编码问题真是坑. 百度mapAPI私钥申请:ht ...
- c语言api获取百度地图,H5调用百度地图API获取地理位置
//获取当前位置 var geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition(function(r){ alert ...
- 百度地图 sdk 使用小结(行动轨迹)
最近把之前的地图代码再看了一遍,想借此机会小结一番,下面是效果图,点没取好,凑合看吧. 这里做的是一个地图上的行为轨迹,包含各个点的覆盖物以及路线绘制,需要用到百度地图定位 SDK,具体如何引入百度地 ...
- 关于百度地图SDK的andriod开发的定位功能实现
基于Android Studio的汽车定位追踪系统的设计 摘要:随着经济的发展和汽车化时代的到来,人们对于车辆的服务的智能化,便捷化提出了更高的要求.文章主要研究通过软件编写一个汽车定位追踪系统,并利 ...
最新文章
- SpringMVC背景介绍及常见MVC框架比较
- Python的全局和局部变量
- 071_html语言代码
- 一个非常棒的jQuery 评分插件--好东西要分享
- java 生成jar_java如何生成jar
- java面试题32:Java网络程序设计中,下列正确的描述是()
- LwIP应用开发笔记之二:LwIP无操作系统UDP服务器
- Python练习:天天向上的力量III
- 基于JAVA+SpringMVC+Mybatis+MYSQL的大学生考勤管理系统及智能分析系统
- mysql日志文件转存_【转】Mysql日志文件
- 【U-net】【粗读】Retinal blood vessel segmentation using fully convolutional network with transfer...
- JavaWeb房屋租赁管理系统(servlet+jsp+mysql)
- 2016SEM行业现状
- 在unity商店中寻找资源_Unity资产商店:五月疯狂促销活动正在进行中!
- Error running' xxxxxx': Command line is too long. Shorten command line for xxxxxxxxx
- 人工智能研究的新前线:生成式对抗网络
- windows 连接蓝牙耳机失败 解决方法
- 贝叶斯统计bayes statistics
- Django + vue 开发接口自动化可视化平台
- 云服务器配置代理服务