Thursday, April 17, 2014

android GPS function problems


Entering the android programming, because of personal love running, intends to prepare a calculation of the GPS applet running distance, but encountered the following problem, adequate guidance:
1, the program starts at the phone, even if the original does not move, the distance will increase (will show tens of meters), is there any way to improve the accuracy of it

2, part of the initialization code onreate:
public void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);

textview1 = (TextView) findViewById (R.id.textView1);
textview2 = (TextView) findViewById (R.id.textView2);
textview3 = (TextView) findViewById (R.id.textView3);
/ / To use the geographic location, you first need to obtain an instance LocationManager in Android in LocationManager is the only way to get through the getSystemService () method is called.
lm = (LocationManager) getSystemService (Context.LOCATION_SERVICE);

/ / Determine whether the normal start
GPSif (! lm.isProviderEnabled (LocationManager.GPS_PROVIDER))
{
/ / Toast floating on the application displays information to the user, it will never get the focus, does not affect the user's input and other operations
/ / Toast.makeText (this, "Please open the GPS navigation ...", Toast.LENGTH_SHORT) show ();.
/ / Use the message box pops up in the form of better
AlertDialog.Builder dialog = new AlertDialog.Builder (this);
dialog.setTitle ("Tip");
dialog.setMessage ("GPS not open, whether to open?");
dialog.setPositiveButton ("yes", dialoglistener);
dialog.setNegativeButton ("NO", dialoglistener);
dialog.show ();
}
lm.requestLocationUpdates (LocationManager.GPS_PROVIDER, 0, 10, locationListener); / / set 10 meters change on update (over one second before setting +10 m, the effect is not good, always beating distance)
}

3, total running distance, the variable value is GPSDistance global double variable is initialized to 0:
private LocationListener locationListener = new LocationListener () {
public void onLocationChanged (Location location) {
weidu2 = location.getLatitude ();
jindu2 = location.getLongitude ();
if (weidu1 == 0 && jindu1 == 0)
{
weidu1 = weidu2;
jindu1 = jindu2;
}
else {
GPSDistance + = GetDistance (weidu1, jindu1, weidu2, jindu2);
String text = "distance (in meters):";
textview1.setText (text);
textview1.append (String.valueOf (GPSDistance));
weidu1 = weidu2;
jindu1 = jindu2;
}
updateView (location) ;/ / This function simply displays the value to textview inside, do not paste the code
reminder ("onlocationchanged of locationlistener");
}

4, calculate the latitude and longitude of two different methods getdistance function:
public double GetDistance (double lat1, double lng1, double lat2, double lng2)
{
float [] results = new float [1];
Location.distanceBetween (lat1, lng1, lat2, lng2, results); / / system comes with a function
return results [0]; / / return distance

}
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
gps itself will drift a bit better in motion. So I think we can begin to determine whether the operator according to the speed.
Reply:
Last night take their own written procedures and third-party software available in the market, such as software and runtastic software to plump contrast, found that the phenomenon is almost the same, even if you stand still does not move, but due to the location of the environment (not enough open) latitude and longitude of the different possibilities leading to a GPS signal receiver has a lot of interference, so the same location or at different times received a lot, but if in motion (over 1 km or so) length calculation was quite accurate GPS, the first written procedures party software is not very different.
Conclusion: I personally believe the GPS signal to stay put or short distance measure is not ideal, but the error is still a long distance test should be acceptable.

Note: There are thought to receive the same location at different times to the latitude and longitude for processing to improve accuracy, but the suspense did not think there any good way.

No comments:

Post a Comment