gpt4 book ai didi

android - 使用 GeoPoint 测量步行距离并向用户显示距离

转载 作者:行者123 更新时间:2023-11-30 03:35:55 25 4
gpt4 key购买 nike

在我的应用程序中,我试图计算一个人步行的距离。为此,我创建了一个 LocationHelper 类,它应该每 30 秒或 10 米获取一次当前 GeoPoint。我已经有一个方法可以返回我知道有效的两个 GeoPoints 之间的距离(以米为单位),因为我在以前的项目中使用过它。我还有一个名为 WalkActivity 的 Activity ,我在 LocationHelper 中调用方法 getDistance() 并将其显示在 TextView 中,该 TextView 在计时器的帮助下每 30 秒更新一次。请参阅下面的代码。

当我运行我的应用程序时,没有任何显示。无论我走多远,它都会说“你已经走了 0.0 米”。我没有收到任何错误消息。你认为我做错了什么?我搜索并查看了许多示例,但没有发现可以告诉我这里出了什么问题。

我希望这不是一个愚蠢的问题,欢迎任何帮助:)

package Controller;

import com.google.android.maps.GeoPoint;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

/**
* Location Helper Class that handles creation of the Location Manager and Location Listener.
*
*/
public class LocationHelper{

private double distance = 0;
GeoPoint geoPointA;
GeoPoint geoPointB;

//location manager and listener
private LocationManager locationManager;
private MyLocationListener locationListener;

/**
* Constructor for LocationHelper
*
* @param context - The context of the calling activity.
*/
public LocationHelper(Context context){

//setup the location manager
locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);

//create the location listener
locationListener = new MyLocationListener();

//setup a callback for when the GPS gets a lock and we receive data
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 10, locationListener);

}

/**
* Receiving notifications from the Location Manager when they are sent.
*
*/
public class MyLocationListener implements LocationListener {


/**
* called when the location service reports a change in location
*/
public void onLocationChanged(Location location) {

if (geoPointB == null){
geoPointB = new GeoPoint((int) location.getLatitude(), (int) location.getLongitude());
}

//Getting the current GeoPoint.
geoPointA = new GeoPoint((int) location.getLatitude(), (int) location.getLongitude());

//Calculating the distance in meters
distance = distance + nu.placebo.whatsup.util.Geodetics.distance(geoPointA, geoPointB);

//Making current GeoPoint the previous GeoPoint
geoPointB = geoPointA;

}

//called when the provider is disabled
public void onProviderDisabled(String provider) {}
//called when the provider is enabled
public void onProviderEnabled(String provider) {}
//called when the provider changes state
public void onStatusChanged(String provider, int status, Bundle extras) {}
}

/**
* Stop updates from the Location Service.
*/
public void killLocationServices(){
locationManager.removeUpdates(locationListener);
}

/**
* Get Distance
*
* @return - The current distance walked.
*/
public double getDistance(){
return distance;
}

/**
* Check if a location has been found yet.
* @return - True if a location has been acquired. False otherwise.
*/
public Boolean gpsEnabled(){
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
}

package edu.chl.dat255.sofiase.readyforapet;

import java.util.Timer;
import java.util.TimerTask;
import Controller.LocationHelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class WalkActivity extends Activity{

private TextView displayDistance;
private int delay = 0;
private int period = 30000;
private Timer timer;
Handler handler = new Handler();

private LocationHelper location;

/**
* On Create method
*
* @param savedInstanceState - bundle
*/
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView(R.layout.walkactivity);
location = new LocationHelper(this);

//Checking if the GPS is enabled, else let the user start GPS if wanted.
if (location.gpsEnabled()){
Toast.makeText(this, "GPS is Enabled on your devide", Toast.LENGTH_SHORT).show();
}
else{
showGPSDisabledAlert();
}

Button startWalking = (Button) findViewById(R.id.startwalking);
startWalking.setOnClickListener(new OnClickListener() {

/**
* Method onClick for the start walking button
*
* @param v - View
*/
public void onClick (View v){

try{
timer = new Timer();
timer.schedule(myTimerTask, delay, period);
}
catch (Exception e){
e.printStackTrace();
}
}
}

);

Button stopWalking = (Button) findViewById(R.id.stopwalking);
stopWalking.setOnClickListener(new OnClickListener() {

/**
* Method onClick for the stop walking button
*
* @param v - View
*/
public void onClick (View v){
timer.cancel();
location.killLocationServices();
startActivity(new Intent(WalkActivity.this, PetActivity.class));
}
}
);

}


TimerTask myTimerTask = new TimerTask() {

@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
displayDistance = (TextView) findViewById(R.id.distance);
displayDistance.setText("You have walked " + location.getDistance() + " meters so far.");
}
});

}

};

/**
* If GPS is turned off, lets the user either choose to enable GPS or cancel.
*
*/
private void showGPSDisabledAlert(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("GPS is disabled on your device. Would you like to enable it?")
.setCancelable(false)
.setPositiveButton("Go to Settings Page To Enable GPS",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(callGPSSettingIntent);
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}

}

最佳答案

代码中的一个问题是当您在 onLocationChanged(Location location) 中从 Location 创建 GeoPoint 时。 Location 类中的 getLatitude()getLongitude() 都返回度数,但 GeoPoint 的构造函数采用微度数.这可能会搞砸您的距离计算,因为您的 GeoPoint 坐标可能是 42 度,在 GeoPoint 中结果是 .0000042 度。

此外,您可以尝试通过调试监视器或通过 Toast 消息以某种方式打印当前位置。这将极大地帮助调试过程,因为您可以从那里看到纬度/经度的变化和调试。

注意:我建议更新到 Maps v2,他们更改了很多类并添加了 LatLng,这让事情更容易理解:)

关于android - 使用 GeoPoint 测量步行距离并向用户显示距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16650929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com