Java 使用Gabe Sechan的教程在android中查找位置
我曾尝试使用androidhive的标准教程查找位置,但在某些时候失败。因此,我在该链接上找到了另一个教程: 他使用两个类来实现接口: 第一类:Java 使用Gabe Sechan的教程在android中查找位置,java,android,Java,Android,我曾尝试使用androidhive的标准教程查找位置,但在某些时候失败。因此,我在该链接上找到了另一个教程: 他使用两个类来实现接口: 第一类: package com.example.marinamapseg; import android.content.Context; import android.location.Location; import android.location.LocationManager; public class FallbackLocationTrack
package com.example.marinamapseg;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
public class FallbackLocationTracker implements LocationTracker, LocationTracker.LocationUpdateListener {
private boolean isRunning;
private ProviderLocationTracker gps;
private ProviderLocationTracker net;
private LocationUpdateListener listener;
Location lastLoc;
long lastTime;
public FallbackLocationTracker(Context context) {
gps = new ProviderLocationTracker(context, ProviderLocationTracker.ProviderType.GPS);
net = new ProviderLocationTracker(context, ProviderLocationTracker.ProviderType.NETWORK);
}
public void start(){
if(isRunning){
//Already running, do nothing
return;
}
//Start both
gps.start(this);
net.start(this);
isRunning = true;
}
public void start(LocationUpdateListener update) {
start();
listener = update;
}
public void stop(){
if(isRunning){
gps.stop();
net.stop();
isRunning = false;
listener = null;
}
}
public boolean hasLocation(){
//If either has a location, use it
return gps.hasLocation() || net.hasLocation();
}
public boolean hasPossiblyStaleLocation(){
//If either has a location, use it
return gps.hasPossiblyStaleLocation() || net.hasPossiblyStaleLocation();
}
public Location getLocation(){
Location ret = gps.getLocation();
if(ret == null){
ret = net.getLocation();
}
return ret;
}
public Location getPossiblyStaleLocation(){
Location ret = gps.getPossiblyStaleLocation();
if(ret == null){
ret = net.getPossiblyStaleLocation();
}
return ret;
}
public void onUpdate(Location oldLoc, long oldTime, Location newLoc, long newTime) {
boolean update = false;
//We should update only if there is no last location, the provider is the same, or the provider is more accurate, or the old location is stale
if(lastLoc == null){
update = true;
}
else if(lastLoc != null && lastLoc.getProvider().equals(newLoc.getProvider())){
update = true;
}
else if(newLoc.getProvider().equals(LocationManager.GPS_PROVIDER)){
update = true;
}
else if (newTime - lastTime > 5 * 60 * 1000){
update = true;
}
if(update){
lastLoc = newLoc;
lastTime = newTime;
if(listener != null){
listener.onUpdate(lastLoc, lastTime, newLoc, newTime);
}
}
}
}
第二类:
package com.example.marinamapseg;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class ProviderLocationTracker implements LocationListener, LocationTracker {
// The minimum distance to change Updates in meters
private static final long MIN_UPDATE_DISTANCE = 0;
// The minimum time between updates in milliseconds
private static final long MIN_UPDATE_TIME = 0;
private LocationManager lm;
public enum ProviderType{
NETWORK,
GPS
};
private String provider;
private Location lastLocation;
private long lastTime;
private boolean isRunning;
private LocationUpdateListener listener;
public ProviderLocationTracker(Context context, ProviderType type) {
lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
if(type == ProviderType.NETWORK){
provider = LocationManager.NETWORK_PROVIDER;
}
else{
provider = LocationManager.GPS_PROVIDER;
}
}
public void start(){
if(isRunning){
//Already running, do nothing
return;
}
//The provider is on, so start getting updates. Update current location
isRunning = true;
lm.requestLocationUpdates(provider, MIN_UPDATE_TIME, MIN_UPDATE_DISTANCE, this);
lastLocation = null;
lastTime = 0;
return;
}
public void start(LocationUpdateListener update) {
start();
listener = update;
}
public void stop(){
if(isRunning){
lm.removeUpdates(this);
isRunning = false;
listener = null;
}
}
public boolean hasLocation(){
if(lastLocation == null){
return false;
}
if(System.currentTimeMillis() - lastTime > 5 * MIN_UPDATE_TIME){
return false; //stale
}
return true;
}
public boolean hasPossiblyStaleLocation(){
if(lastLocation != null){
return true;
}
return lm.getLastKnownLocation(provider)!= null;
}
public Location getLocation(){
if(lastLocation == null){
return null;
}
if(System.currentTimeMillis() - lastTime > 5 * MIN_UPDATE_TIME){
return null; //stale
}
return lastLocation;
}
public Location getPossiblyStaleLocation(){
if(lastLocation != null){
return lastLocation;
}
return lm.getLastKnownLocation(provider);
}
public void onLocationChanged(Location newLoc) {
long now = System.currentTimeMillis();
if(listener != null){
listener.onUpdate(lastLocation, lastTime, newLoc, now);
}
lastLocation = newLoc;
lastTime = now;
}
public void onProviderDisabled(String arg0) {
}
public void onProviderEnabled(String arg0) {
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
但问题是,他没有提到如何在android应用程序中使用以下两个类
FallbackLocationTracker fobj;
fobj=new FallbackLocationTracker(MainActivity.this);
fobj.start();
if(fobj.hasLocation())
{
Location locobj=fobj.getLocation();
current_latitude=locobj.getLatitude();
current_longitude=locobj.getLongitude();
Toast.makeText(getApplicationContext(),current_latitude+" "+current_longitude,Toast.LENGTH_LONG).show();
placemarkersonmap();
}
当我调试代码时,hasLocation方法返回false,因为第一个类中的onLocationChanged方法只有在调用hasLocation方法之后才被调用。因此lastlocation对象将为null,并返回false
但是为什么不在以下线路运行时立即调用onlocationchanged:
lm.requestLocationUpdates(provider, MIN_UPDATE_TIME, MIN_UPDATE_DISTANCE, this);
或者有没有什么方法可以告诉我如何使用代码。请帮助 onLocationChanged是一个回调,这意味着它是事件驱动的,在设备检测到位置更改之前不会运行。如果您仅从GPS获取位置,则可能需要几分钟才能获得定位。如果您在室内看不到清晰的天空,则它可能永远不会运行。onLocationChanged是一个回调,这意味着它是事件驱动的,在设备检测到位置更改之前不会运行。如果您仅从GPS获取位置,则可能需要几分钟才能获得定位。如果你在室内看不到清晰的天空,那么它可能永远不会运行。你可以在Activity的onCreate方法中添加以下内容: @凌驾 受保护的void onCreateBundle savedInstanceState{ super.onCreatesavedInstanceState; setContentViewR.layout.activity_main; textViewHeading=TextView this.findviewbydr.id.text\u heading; textViewLong=TextView this.findviewbydr.id.text\u long; textViewLat=TextView this.findViewByIdR.id.text\u lat; textViewHeight=TextView this.findViewByIdR.id.text\u height; textViewAccurance=TextView this.findViewByIdR.id.text\u准确性; 计数器=长0; gps=新供应商位置跟踪器此, ProviderLocationTracker.ProviderType.GPS; listener=新位置UpdateListener{ @凌驾 更新位置oldLoc、long oldTime、位置newLoc上的公共无效, 长时间{ //TODO自动生成的方法存根 计数器=计数器+长1; 如果gps.HASSLOCATION{ text1=GPS具有位置+计数器。toString+; }否则{ text1=GPS没有位置+计数器。toString+; } 如果gps.HAS有可能的结晶位置{ text1=text1+加上陈旧位置; } 双经度=newLoc.getLongitude; 双纬度=newLoc.getLatitude; 双倍高度=新定位高度; 浮动精度=newLoc.getaccurity; text2=经度=+经度.toString; text3=纬度=+纬度.toString; text4=高度=+高度.toString; text5=精度=+精度.toString; textViewHeading.setTexttext1; textViewLong.setTexttext2; textViewLat.setTexttext3; textViewHeight.settextextext4; textViewAccurance.setTexttext5; } }; 全球定位系统;
}//end method onCreate您可以在Activity的onCreate方法中添加以下内容: @凌驾 受保护的void onCreateBundle savedInstanceState{ super.onCreatesavedInstanceState; setContentViewR.layout.activity_main; textViewHeading=TextView this.findviewbydr.id.text\u heading; textViewLong=TextView this.findviewbydr.id.text\u long; textViewLat=TextView this.findViewByIdR.id.text\u lat; textViewHeight=TextView this.findViewByIdR.id.text\u height; textViewAccurance=TextView this.findViewByIdR.id.text\u准确性; 计数器=长0; gps=新供应商位置跟踪器此, ProviderLocationTracker.ProviderType.GPS; listener=新位置UpdateListener{ @凌驾 更新位置oldLoc、long oldTime、位置newLoc上的公共无效, 长时间{ //TODO自动生成的方法存根 计数器=计数器+长1; 如果gps.HASSLOCATION{ text1=GPS具有位置+计数器。toString+; }否则{ text1=GPS没有位置+计数器。toString+; } 如果gps.HAS有可能的结晶位置{ text1=text1+加上陈旧位置; } 双经度=newLoc.getLongitude; 双纬度=newLoc.getLatitude; 双倍高度=新定位高度; 浮动精度=newLoc.getaccurity; text2=经度=+经度.toString; text3=纬度=+纬度.toString; text4=高度=+高度.toString; text5=精度=+精度.toString; textViewHeading.setTexttext1; textViewLong。 Settext2; textViewLat.setTexttext3; textViewHeight.settextextext4; textViewAccurance.setTexttext5; } }; 全球定位系统;
}//end method onCreate这篇文章似乎已经有了一个答案,说明了如何使用ProviderLocationTracker。我把我的答案放在这里,只是想说明使用FallbackLocationTracker是多么容易 我不记得对FallbackLocationTracker做过任何重大更改,因此我不会发布整个代码
public class MainActivity extends AppCompatActivity {
private FallbackLocationTracker tracker;
private Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tracker = new FallbackLocationTracker(MainActivity.this);
}
@Override
protected void onResume() {
super.onResume();
// Check for permission to access location and start location
// tracking if permissions are granted and location services
// are enabled
if (locationPermissionGranted && locationServicesEnabled) {
startLocationUpdates();
} else {
// Handle accordingly
}
}
@Override
protected void onPause() {
super.onPause();
stopLocationUpdates();
}
private void startLocationUpdates() {
tracker.start(new LocationTracker.LocationUpdateListener() {
@Override
public void onUpdate(Location oldLocation, long oldTime,
Location newLocation, long newTime) {
// You will receive updated changes to your location here
location = newLocation;
}
});
}
private void stopLocationUpdates() {
tracker.stop();
}
}
就是这样。这篇文章似乎已经有了一个答案,说明了如何使用ProviderLocationTracker。我把我的答案放在这里,只是想说明使用FallbackLocationTracker是多么容易 我不记得对FallbackLocationTracker做过任何重大更改,因此我不会发布整个代码
public class MainActivity extends AppCompatActivity {
private FallbackLocationTracker tracker;
private Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tracker = new FallbackLocationTracker(MainActivity.this);
}
@Override
protected void onResume() {
super.onResume();
// Check for permission to access location and start location
// tracking if permissions are granted and location services
// are enabled
if (locationPermissionGranted && locationServicesEnabled) {
startLocationUpdates();
} else {
// Handle accordingly
}
}
@Override
protected void onPause() {
super.onPause();
stopLocationUpdates();
}
private void startLocationUpdates() {
tracker.start(new LocationTracker.LocationUpdateListener() {
@Override
public void onUpdate(Location oldLocation, long oldTime,
Location newLocation, long newTime) {
// You will receive updated changes to your location here
location = newLocation;
}
});
}
private void stopLocationUpdates() {
tracker.stop();
}
}
就这样。知道如何使用回退位置跟踪器会很好。知道如何使用回退位置跟踪器会很好。我正要写一个答案,但谢天谢地,你做到了;我正要写一个答案,但谢天谢地你已经写了;