【发布时间】:2014-01-27 15:52:20
【问题描述】:
我正在创建一个应用程序,它使用谷歌地图 v2 和 gps 来计算用户步行的距离。有2个问题。 我的gps不准确。尽管周围环境是正确的,但它并没有准确地显示我在哪里。在道路,建筑物等上,但显示的位置似乎与确切位置有点偏离。 它更新位置的方式是几秒钟后蓝点会从一个地方“跳”到另一个地方。而不是平滑移动并显示用户已运行的线条。 我的距离是不准确的。我需要按开始移动以显示距离。有时我按下它后应用程序崩溃。计数值是让我确定应用程序在我的代码中运行的位置。根据我的计划,随着距离的增加,计数应该会不断增加。 抱歉,如果有一堆信息,但我想我应该提供更多信息。我在互联网上搜索,但找不到计算准确距离或提高 GPS 精度的方法
这是我的 GPS 屏幕截图
我的主要活动 Java 代码
import com.example.mypp.R;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.view.Menu;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity implements LocationListener, Runnable{
protected LocationManager locationManager;
private GoogleMap googleMap;
Button btnStartMove,btnPause,btnResume,btnStop;
TextView Distance;
static double n=0;
Long s1,r1;
double lat1,lon1,lat2,lon2;
double dis=0.0;
MyCount counter;
Thread t1;
EditText e1;
boolean bool=false;
int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if(isGooglePlay())
{
//setContentView(R.layout.activity_main);
setUpMapIfNeeded();
}
btnStartMove=(Button)findViewById(R.id.Start);//start moving
btnPause=(Button)findViewById(R.id.Pause);//pause
btnResume=(Button)findViewById(R.id.Resume);//resume
btnStop=(Button)findViewById(R.id.Stop);
Distance=(TextView)findViewById(R.id.Distance);
btnStartMove.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//start(v);
Distance.setText("Distance:" + dis);
start(v);
}
});
btnPause.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//start(v);
}
});
btnResume.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//start(v);
}
});
btnStop.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//start(v);
}
});
}
private void setUpMapIfNeeded() {
if(googleMap == null)
{
Toast.makeText(MainActivity.this, "Getting map",
Toast.LENGTH_LONG).show();
googleMap =((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.displayMap)).getMap();
if(googleMap != null)
{
setUpMap();
}
}
}
private void setUpMap()
{
//Enable MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
//Get locationManager object from System Service LOCATION_SERVICE
//LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
//Get the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
if(provider == null)
{
onProviderDisabled(provider);
}
//set map type
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//Get current location
Location myLocation = locationManager.getLastKnownLocation(provider);
if(myLocation != null)
{
onLocationChanged(myLocation);
}
}
private boolean isGooglePlay()
{
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (status == ConnectionResult.SUCCESS)
{
Toast.makeText(MainActivity.this, "Google Play Services is available",
Toast.LENGTH_LONG).show();
return(true);
}
else
{
GooglePlayServicesUtil.getErrorDialog(status, this, 10).show();
}
return (false);
}
@Override
public void onLocationChanged(Location myLocation) {
//Get latitude of the current location
double latitude = myLocation.getLatitude();
//Get longitude of the current location
double longitude = myLocation.getLongitude();
//Create a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
//Show the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
//Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(20));
googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!"));
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(MainActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(MainActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(MainActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void start (View v){
switch(v.getId()){
case R.id.Start:
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null)
{
lat2=location.getLatitude();
lon2=location.getLongitude();
bool=true;
t1=new Thread(this);
t1.start();
counter= new MyCount(30000,1000);
counter.start();
}
else
{
Toast.makeText(MainActivity.this, "null location",
Toast.LENGTH_LONG).show();
}
break;
case R.id.Pause:
counter.cancel();
bool=false;
break;
case R.id.Resume:
counter= new MyCount(s1,1000);
counter.start();
bool=true;
break;
case R.id.Distance:
double time=n*30+r1;
Distance.setText("Distance:" + dis);
Toast.makeText(MainActivity.this,"distance in metres:"+String.valueOf(dis)+"Velocity in m/sec :"+String.valueOf(dis/time)+"Time :"+String.valueOf(time),Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, "Value of Count:" + String.valueOf(count),
Toast.LENGTH_LONG).show();
break;
case R.id.Stop:
counter.cancel();
counter= new MyCount(30000,1000);
bool = false;
}
}
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
counter= new MyCount(30000,1000);
counter.start();
n=n+1;
}
@Override
public void onTick(long millisUntilFinished) {
s1=millisUntilFinished;
r1=(30000-s1)/1000;
e1.setText(String.valueOf(r1));
}
}
public double getDistance(double lat1, double lon1, double lat2, double lon2) {
double latA = Math.toRadians(lat1);
double lonA = Math.toRadians(lon1);
double latB = Math.toRadians(lat2);
double lonB = Math.toRadians(lon2);
double cosAng = (Math.cos(latA) * Math.cos(latB) * Math.cos(lonB-lonA)) +
(Math.sin(latA) * Math.sin(latB));
double ang = Math.acos(cosAng);
double dist = ang *6371;
return dist;
}
@Override
public void run()
{
count=6;
//LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
lat1=location.getLatitude();
lon1=location.getLongitude();
while(bool==true)
{
if(lat1!=lat2 || lon1!=lon2)
{
dis+=getDistance(lat2,lon2,lat1,lon1);
lat2=lat1;
lon2=lon1;
count =7;
Distance.setText("Distance: " + dis + " Count: " + count);
count++;
}
}
}}
抱歉,我忘了过去的崩溃日志。这里是。我知道我的代码很多且令人困惑,但我不知道计算距离的有效方法。
01-28 00:36:58.820: D/dalvikvm(25535): GC_CONCURRENT freed 595K, 8% free 14911K/16135K, paused 4ms+8ms, total 56ms
01-28 00:37:06.135: D/TextLayoutCache(25535): TextLayoutCache::replaceThai, prevBuffer[0] is 30
01-28 00:37:06.135: D/TextLayoutCache(25535): TextLayoutCache::replaceThai, prevBuffer[0] is 30
01-28 00:37:06.135: D/TextLayoutCache(25535): TextLayoutCache::replaceThai, prevBuffer[1] is 2e
01-28 00:37:06.135: D/TextLayoutCache(25535): TextLayoutCache::replaceThai, prevBuffer[0] is 30
01-28 00:37:06.135: D/TextLayoutCache(25535): TextLayoutCache::replaceThai, prevBuffer[1] is 2e
01-28 00:37:06.135: D/TextLayoutCache(25535): TextLayoutCache::replaceThai, prevBuffer[2] is 30
01-28 00:37:06.135: D/TextLayoutCache(25535): TextLayoutCache::replaceThai, prevBuffer[0] is 30
01-28 00:37:06.135: D/TextLayoutCache(25535): TextLayoutCache::replaceThai, prevBuffer[0] is 30
01-28 00:37:06.135: D/TextLayoutCache(25535): TextLayoutCache::replaceThai, prevBuffer[1] is 2e
01-28 00:37:06.135: D/TextLayoutCache(25535): TextLayoutCache::replaceThai, prevBuffer[0] is 30
01-28 00:37:06.135: D/TextLayoutCache(25535): TextLayoutCache::replaceThai, prevBuffer[1] is 2e
01-28 00:37:06.135: D/TextLayoutCache(25535): TextLayoutCache::replaceThai, prevBuffer[2] is 30
01-28 00:37:06.140: D/AndroidRuntime(25535): Shutting down VM
01-28 00:37:06.140: W/dalvikvm(25535): threadid=1: thread exiting with uncaught exception (group=0x4178f2a0)
01-28 00:37:06.140: E/AndroidRuntime(25535): FATAL EXCEPTION: main
01-28 00:37:06.140: E/AndroidRuntime(25535): java.lang.NullPointerException
01-28 00:37:06.140: E/AndroidRuntime(25535): at com.example.studenthealthapp.MainActivity$MyCount.onTick(MainActivity.java:262)
01-28 00:37:06.140: E/AndroidRuntime(25535): at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
01-28 00:37:06.140: E/AndroidRuntime(25535): at android.os.Handler.dispatchMessage(Handler.java:99)
01-28 00:37:06.140: E/AndroidRuntime(25535): at android.os.Looper.loop(Looper.java:137)
01-28 00:37:06.140: E/AndroidRuntime(25535): at android.app.ActivityThread.main(ActivityThread.java:4898)
01-28 00:37:06.140: E/AndroidRuntime(25535): at java.lang.reflect.Method.invokeNative(Native Method)
01-28 00:37:06.140: E/AndroidRuntime(25535): at java.lang.reflect.Method.invoke(Method.java:511)
01-28 00:37:06.140: E/AndroidRuntime(25535): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
01-28 00:37:06.140: E/AndroidRuntime(25535): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
01-28 00:37:06.140: E/AndroidRuntime(25535): at dalvik.system.NativeStart.main(Native Method)
01-28 00:37:14.305: I/Process(25535): Sending signal. PID: 25535 SIG: 9
【问题讨论】:
-
你的问题不清楚,还有很多代码,你也提到了崩溃,但没有提供与这次崩溃相关的堆栈跟踪。
标签: android google-maps gps distance