Android—定位
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button bt_main_checkin;
private Toolbar toolbar;
private TextView tv_main_test;
private LocationManager mLocationManager;
private static final int TEN_SECONDS = 10000;
private static final int TEN_METERS = 10;
private static final int TWO_MINUTES = 1000 * 60 * 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
bt_main_checkin.setOnClickListener(this);
setSupportActionBar(toolbar);
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location LatLng = setup();
// Toast.makeText(this, LatLng.getLatitude() + "|" + LatLng.getLongitude(), Toast.LENGTH_SHORT).show();
tv_main_test.setText(LatLng.getLatitude() + "|" + LatLng.getLongitude());
}
private void initView() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
bt_main_checkin = (Button) findViewById(R.id.bt_main_checkin);
tv_main_test= (TextView) findViewById(R.id.tv_main_test);
}
@Override
protected void onResume() {
super.onResume();
setup();
}
@Override
protected void onStop() {
super.onStop();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLocationManager.removeUpdates(listener);
}
@Override
protected void onStart() {
super.onStart();
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
final boolean gpsenable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!gpsenable) {
new EnableGpsDialogFragment().show(getFragmentManager(), "enableGpsDialog");
}
}
@Override
public void onClick(View v) {
}
private Location setup() {
Location gpsLocation = null;
Location networkLocation = null;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
new EnableGpsDialogFragment().show(getFragmentManager(), "enableGpsDialog");
new EnableNetworkDialogFragment().show(getFragmentManager(), "enableNetworkDialog");
return null;
}
mLocationManager.removeUpdates(listener);
gpsLocation = requestUpdatesFromProvider(LocationManager.GPS_PROVIDER, R.string.not_support_gps);
networkLocation = requestUpdatesFromProvider(LocationManager.NETWORK_PROVIDER, R.string.not_support_network);
if (gpsLocation != null && networkLocation != null) {
return getBetterLocation(gpsLocation, networkLocation);
} else if (gpsLocation != null) {
return gpsLocation;
} else if (networkLocation != null) {
return networkLocation;
}
return null;
}
private Location requestUpdatesFromProvider(final String provider, final int errorResId) {
Location location = null;
if (mLocationManager.isProviderEnabled(provider)) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
return null;
}
mLocationManager.requestLocationUpdates(provider, TEN_SECONDS, TEN_METERS, listener);
location = mLocationManager.getLastKnownLocation(provider);
} else {
Toast.makeText(this, errorResId, Toast.LENGTH_SHORT).show();
}
return location;
}
private final LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// TODO: 2016/3/23 updata ui in it
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
protected Location getBetterLocation(Location newLocation, Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return newLocation;
}
// If it's been more than two minutes since the current location, use the new location
// because the user has likely moved.
long timeDelta = newLocation.getTime() - currentBestLocation.getTime();
boolean isSignficantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignficantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;
if (isSignficantlyNewer) {
return newLocation;
} else if (isSignficantlyOlder) {
return currentBestLocation;
}
// Determine location quality using a combination of timeliness and accuracy
int accuracyDelta = (int) (newLocation.getAccuracy() - currentBestLocation.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
boolean isFromSameProvider = isSameProvider(newLocation.getProvider(),
currentBestLocation.getProvider());
if (isMoreAccurate) {
return newLocation;
} else if (isNewer && !isLessAccurate) {
return newLocation;
} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
return newLocation;
}
return currentBestLocation;
}
/**
* Checks whether two providers are the same
*/
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id){
case R.id.action_settings:
break;
case R.id.action_register:
break;
default:
break;
}
//noinspection SimplifiableIfStatement
// if (id == R.id.action_settings) {
// return true;
// }
return true;
}
}
详细见官网
Android—定位的更多相关文章
- android 定位的四种方式
[原文] 开发中对于地图及地理位置的定位是我们经常要用地,地图功能的使用使得我们应用功能更加完善,下面总结了一下网络中现有对于介绍android定位的4种方式,希望对大家有帮助: android 定 ...
- Android定位功能
不说废话,直接说说实现android定位有关的API吧. 这些API都在android.location包下,一共有三个接口和八个类.它们配合使用即可实现定位功能. 三个接口: GpsStatus.L ...
- Android定位功能(二)
在前文Android定位功能(一)中,已经大致介绍了一下在Android平台中,和定位功能相关的类,并举例获取了位置信息.但是前文是基于Criteria定制了一个标准,通过getBestProvide ...
- Android定位测试(深坑)
问题:我们是一个海外app,市场部去马来西亚打开那边的市场,发现了一个问题,就是我们的app定位有问题,还是成都的定位,主要原因是在马来西亚使用这个app,请求中带的经纬度参数是成都的,导致服务器返回 ...
- android 定位的几种方式介绍
[地理位置] android 定位的几种方式介绍 开发中对于地图及地理位置的定位是我们经常要用地,地图功能的使用使得我们应用功能更加完善,下面 www.androidkaifa.com 总结了一下网络 ...
- 六 APPIUM Android 定位方式
文本转自:http://www.cnblogs.com/sundalian/p/5629500.html APPIUM Android 定位方式 1.定位元素应用元素 1.1通过id定位元素 An ...
- 发现最新版百度Android 定位SDK v6.1.3 网络定位bug
对于百度地图已经实在忍无可忍了,实验室两年以前的一个项目用到了百度地图,以前师兄毕业了,我来维护这个破项目,百度地图推出新版本出来后,老版本的api不能用了,不能做到向下兼容吗?换掉少量的api也就算 ...
- Appium学习路—Android定位元素与操作
一.常用识别元素的工具 uiautomator:Android SDK自带的一个工具,在tools目录下 monitor:Android SDK自带的一个工具,在tools目录下 Appium Ins ...
- 目前主流的Android定位有如下几种:
1.通过GPS模块 GPS方式准确度是最高的,但是它的缺点也非常明显:1,比较耗电:2,绝大部分用户默认不开启GPS模块:3,从GPS模块启动到获取第一次定位数据,可能需要比较长的时间:4,室内几乎无 ...
- android定位
先说说手机定位的方式 1,GPS 绝大部分手机都有GPS模块,这种方式准确度是最高的,但是缺点也很明显,1,耗电高:2,绝大部分用户默认不开启GPS模块.3,从GPS模块启动到获取第一次定位数据,可能 ...
随机推荐
- 梯度下降(Gradient Descent)小结
在求解机器学习算法的模型参数,即无约束优化问题时,梯度下降(Gradient Descent)是最常采用的方法之一,另一种常用的方法是最小二乘法.这里就对梯度下降法做一个完整的总结. 1. 梯度 在微 ...
- golang中的类和接口的使用
类使用:实现一个people中有一个sayhi的方法调用功能,代码如下: type People struct { //.. } func (p *People) SayHi() { fmt.Prin ...
- WebApi Controller 分类
前言 写这篇WebApi Controller分类一是把Contrller分类的解决方法说一说,再顺便把遇到的问题和解决方法聊一聊. 说实话第一次在项目中使用WebApi感觉非常的不顺手. 遇到的问题 ...
- 【原创】轻量级移动端即时通讯技术 MobileIMSDK 发布了
申明:MobileIMSDK目前为个人原创开源工程,投入了大量的时间和精力,希望对需要的人有所帮助.如需与作者交流,见文章底部个人签名处,互相学习.Q群:215891622,欢迎共同志趣者学习和交流. ...
- learn shell
the basic shell skills. Bourne shell sh ksh Bash psh zsh C shell csh tcsh [root@bogon temp]# cat ...
- 小白Linux入门 一
1 win7电脑上安装虚拟机,准备文件 vmware 12 http://www.orsoon.com/Soft/89658.html ubuntu 16.04 http://cn.ubuntu.c ...
- 关于从JSP页面插入数据到数据库中乱码问题的解决
问题描述:最近我在写一个j2ee的留言板系统模块,遇到了一个非常让我头大的问题,当我从JSP页面输入数据后,通过hibernate中的业务逻辑类HQL语句把这个数据插入到本地的mysql数据库中,可是 ...
- js赋值运算的理解
简介 js引擎由于为了效率,很多时候的非直接量赋值都不是copy一份在赋值给新的变量,而是一个引用 ps:直接量:直接值数字字符串等 为什么使用len = doms.length; 里的len效率要比 ...
- 针对Asp.net MVC SEO的几点建议
1. 引言 SEO 即搜索引擎优化,很多web开发人员本应该熟悉,至少需要了解的一个知识点.像百度.必应等搜索引擎其实一直都在进化.但是有些优化的技巧可能在短时间内不变. 今天就给大家介绍几个专门针对 ...
- Differences between INDEX, PRIMARY, UNIQUE, FULLTEXT in MySQL?
487down vote Differences KEY or INDEX refers to a normal non-unique index. Non-distinct values for ...