我正在尝试从最好的提供商那里获取位置。我启用了GPS。此外,当我打印纬度和经度时,我从网络提供商那里获取它。
我的问题是:
如果启用了GPS,那么我想通过GPS搜索30秒的位置。在那之后,如果我的精度低于200米,那么我就使用它。如果精度超过200米,我会再次搜索,然后从网络提供商那里开始。
之后,我比较两者的准确性,并获取更准确的提供商的数据。
这是我的代码:
LocationUtil。JAVA
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
public class LocationUtil
{
Activity activity;
Location location;
public LocationUtil(Activity activity)
{
this.activity = activity;
}
public int getLogitudeE6()
{
LocationManager lm = (LocationManager) activity
.getSystemService(Context.LOCATION_SERVICE);
Location location = lm
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
int lg = (int) (((double) location.getLongitude()) * 1E6);
System.out.println("Longitude :: " + lg);
return lg;
}
public int getLatitudeE6()
{
LocationManager lm = (LocationManager) activity
.getSystemService(Context.LOCATION_SERVICE);
Location location = lm
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
int lt = (int) (((double) location.getLatitude()) * 1E6);
System.out.println("Latitude :: " + lt);
return lt;
}
public double getLogitude(Location location)
{
if (location == null)
{
LocationManager lm = (LocationManager) activity
.getSystemService(Context.LOCATION_SERVICE);
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null)
{
location = lm
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
this.location = location;
}
return location.getLongitude();
}
public double getLatitude(Location location)
{
if (location == null)
{
LocationManager lm = (LocationManager) activity
.getSystemService(Context.LOCATION_SERVICE);
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null)
{
location = lm
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
this.location = location;
}
return location.getLatitude();
}
public double getAccuracy(Location location)
{
if (location == null)
{
LocationManager lm = (LocationManager) activity
.getSystemService(Context.LOCATION_SERVICE);
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null)
{
location = lm
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
this.location = location;
}
return location.getAccuracy();
}
public double getLogitude()
{
if (location == null)
{
LocationManager lm = (LocationManager) activity
.getSystemService(Context.LOCATION_SERVICE);
Location location = lm
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
this.location = location;
}
return location.getLongitude();
}
public double getLatitude()
{
if (location == null)
{
LocationManager lm = (LocationManager) activity
.getSystemService(Context.LOCATION_SERVICE);
Location location = lm
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
this.location = location;
}
return location.getLatitude();
}
}
CaptureMain。JAVA
import java.util.Timer;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class CaptureMain extends Activity implements LocationListener {
/** Called when the activity is first created. */
TextView txtNewLocation;
String strLatBack, strLongBack, strLatitude, strLongitude,strAccuracy;
LocationUtil locationUtil;
Location location;
LocationManager lm;
String bestProvider;
Timer timer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtNewLocation = (TextView) findViewById(R.id.txtLocation);
locationUtil = new LocationUtil(this);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
try {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
bestProvider = lm.getBestProvider(criteria, false);
Log.i("Log", "Best provider is : "+bestProvider);
strLatitude = String.valueOf(locationUtil.getLatitude(location));
strLongitude = String.valueOf(locationUtil.getLogitude(location));
strAccuracy = String.valueOf(locationUtil.getAccuracy(location));
txtNewLocation.setText("Latitude :" + strLatitude + ", Longitude :"
+ strLongitude + " , Accuracy "+ strAccuracy);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onLocationChanged(Location location) {
System.out.println("On Location change called:: ");
if (location != null) {
this.location = location;
}
strLatitude = String.valueOf(locationUtil.getLatitude(location));
strLongitude = String.valueOf(locationUtil.getLogitude(location));
strAccuracy = String.valueOf(locationUtil.getAccuracy(location));
if (strLatitude.equals("") || strLongitude.equals("")) {
txtNewLocation.setText("Locating current position..");
} else {
txtNewLocation.setText("Latitude :" + strLatitude + ", Longitude :"
+ strLongitude + " , Accuracy "+ strAccuracy);
}
}
@Override
protected void onStart() {
super.onStart();
startListening();
}
@Override
protected void onResume() {
super.onResume();
startListening();
}
@Override
protected void onPause() {
super.onPause();
stopListening();
}
@Override
protected void onDestroy() {
super.onDestroy();
stopListening();
finish();
}
private void stopListening() {
if (lm != null)
lm.removeUpdates(this);
}
private void startListening() {
lm.requestLocationUpdates(bestProvider, 0, 0, this);
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
在清单中添加了权限:
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
我还没有详细研究过你的算法,但200米不是GPS的好阈值。水平精度应在50米以下。
只有GPS有属性路线和高度(我不是100%确定)和速度。也检查有效的高度和路线。路线(和速度)仅在设备移动时有效。
但你能不能不只是简单地查询位置提供者的类型?(全球定位系统)
我正在尝试开发一个android应用程序,该应用程序使用GPS或网络提供商获取设备纬度和经度,以该过程可用的为准。当设备的GPS打开时,代码工作得非常好,但不幸的是,当设备的GPS关闭时,它永远不会工作。 例如,我想根据可用的提供商获取设备的纬度和经度;如果设备的GPS打开,它应该使用GPS提供商获取设备的纬度和经度,如果GPS关闭,它应该使用网络提供商获取设备的纬度和经度。在我的情况下,网络提供
Galaxy S3-GPS图标没有出现,它没有启动onLocationChanged方法。(好吧) Htc one M8-GPS图标出现,闪烁,并触发onlocationchange(但如何?) 这时问题突然出现 null null
其次,检查GPS是否启用,以及GPS提供商是否工作正常,我可以从GPS提供商那里获取纬度和经度。 第三,如果GPS关闭,我试图从网络提供商那里获得位置。但是,在这种情况下有时我不能成功地获取位置。 所以,我必须让用户打开GPS,并且在打开GPS后必须得到位置。
从priority_balanced_power_precission给出的精确度有多高得到了这个? 高精度模式使用所有位置提供商,但是,它优先考虑位置提供商,并包括GPS和位置提供商。定位精度大约在10米范围内。 NO_POWER不使用任何位置提供商,而是从其他应用程序获取位置的被动模式。精确度可能是一英里或更多。它完全基于其他应用程序最近获取的位置。 Priority_balanced_pow
我已经在我的清单中定义了使用gps的所有前置任务。喜欢 “使用权限android:name=“android。准许进入“精细位置”/ "使用权限android: name="android.permission.ACCESS_COARSE_LOCATION"/ 每次我尝试在Android 6.0上打开此活动时,它都会以此logcat消息停止。但在一些手机上也能用。有人能帮我弄清楚,并提供解决方案吗
PagodaBox AppFog Heroku fortrabbit Engine Yard Cloud Red Hat OpenShift Platform AWS Elastic Beanstalk Windows Azure Google App Engine Jelastic Platform.sh Cloudways IBM Bluemix Cloud Foundry Pivotal W