确保位置准确性并在Google地图上跟踪用户

时间:2014-02-09 05:06:34

标签: android location google-maps-api-2

我的Android应用程序功能是在行走/跑步或者从不移动时跟踪用户位置(然后位置不应该改变)首先,位置不准确,即使我从未移动过,红色标记也会在地图上弹出。弹出的位置也是不准确的。

对于之前使用类似位置跟踪应用的用户,蓝点应该与最新的红色标记位置相同吗?但是我的蓝点总是在大部分时间都不在。红色标记显示更准确的位置(虽然仍然不准确)

我已经在我的房子里,外面,在车里测试过它。当它在我家时,有时位置不断变化,有时位置永远不会改变。我无法预测它。通常gps在内部工作不好所以我先把它留下来 当我在外面测试它时,有时它会更好,但它仍然是不准确的。当我从不移动/移动或移动缓慢时,它会在错误的位置更新。虽然它有时候比屋顶好一点。但最终还是无法使用。 当我在车上测试它时,它会更好,因为它不会弹出那么多位置应该是更准确的蓝点和红色标记有时在一起。虽然有时仍然不准确。但是当我的应用程序在用户步行/跑步而不是在移动的车辆中时应该使用它并不重要。

对于所有三个,红色标记有时会出现在正确的环境(房屋,道路,某些建筑物)周围(无论多么频繁),但蓝点不是,大多数情况下都是错误的。但我希望蓝点作为用户的跟踪器。用户不知道哪个红色标记是他当前的位置,即使它是准确的。标题“你在这里”可以出现在任何标记上,当我点击它时,它是新的还是旧的。所以它没有效果。当出现新标题时,标题本身不会出现。

我试图解决的问题是确保定位准确性,即使不是很好或完美无法使用,并且有蓝点跟踪用户连续,或红色标记,只要有时告诉他在哪里准确(标题仅出现在最新标记上)。

我研究了很长时间,我不知道我还能做些什么来使它更准确。我认为距离计算应该是准确的,但这取决于位置,所以它也会变得不准确。对不起,我认为我应该更清楚地解释一下大文...

 public class MainActivity extends FragmentActivity implements LocationListener{

protected LocationManager locationManager;
private GoogleMap googleMap;
Button btnStartMove,btnPause,btnResume,btnStop;
static double n=0;
Long s1,r1;
double dis=0.0;
Thread t1;
EditText userNumberInput;
boolean bool=false;
int count=0;

double speed = 1.6;
double lat1,lon1,lat2,lon2,lat3,lon3,lat4,lon4;
double dist = 0.0;
double time = 0.0;
double velocity = 0.0;
TextView distance;
Button btnDuration;
float[] result;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES =1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 4000; //in milliseconds
boolean startDistance = false;
boolean startButtonClicked = false;
MyCount counter;
int timer = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, this);
    if(isGooglePlay())
    {
        setUpMapIfNeeded();
    }
    distance=(TextView)findViewById(R.id.Distance);
    btnDuration=(Button)findViewById(R.id.Duration);
    btnStartMove=(Button)findViewById(R.id.Start);//start moving
    btnStop=(Button)findViewById(R.id.Stop);

    //prepare distance...........
    Log.d("GPS Enabled", "GPS Enabled");  
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    String provider = locationManager.getBestProvider(criteria, true);
    Location location=locationManager.getLastKnownLocation(provider);

    btnStartMove.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) {

            Log.d("GPS Enabled", "GPS Enabled");  
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            String provider = locationManager.getBestProvider(criteria, true);
            Location location=locationManager.getLastKnownLocation(provider);
            lat3 = location.getLatitude();
            lon3 = location.getLongitude();
            startButtonClicked=true;
            startDistance=true;
            counter= new MyCount(30000,1000);
            counter.start();
            btnStartMove.setText("Started...");
        }
    });
    btnStop.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) {
            startButtonClicked=false;
            startDistance=false;
            //Double.valueOf(distance.getText().toString()
            Double value=dist;
            Double durationValue=time;
            Double speedValue=velocity;
            Intent intent = new Intent(MainActivity.this, FinishActivity.class);

            intent.putExtra("dist", value);
            intent.putExtra("time",durationValue);
            intent.putExtra("velocity",speedValue);
            startActivity(intent);
            counter.cancel();
            n=0;
            r1=null;
            time=0.0;
            btnStartMove.setText("Start Move");
            finish();
        }
    });

    btnDuration.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) {
            if(startButtonClicked=true) 
            {
                time=n*30+r1;
                velocity=dist/time;
                DecimalFormat df = new DecimalFormat("#.##");
                Toast.makeText(MainActivity.this,"Duration :"+String.valueOf(time) + "Speed :"+String.valueOf(df.format(velocity)),Toast.LENGTH_LONG).show();

            }       
        }
    });

    if(location!= null)
    {
        //Display current location in Toast
        String message = String.format(
                "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude()
        );
        Toast.makeText(MainActivity.this, message,
                Toast.LENGTH_LONG).show();
    }
    else if(location == null)
    {
        Toast.makeText(MainActivity.this,
                "Location is null",    
                Toast.LENGTH_LONG).show();
    }


}
private void setUpMapIfNeeded() {

    if(googleMap == null)
    {
        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();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    //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)
    {
        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)).snippet("You are here!").title("You are here!"));
    }       
    locationManager.requestLocationUpdates(provider, 0, 0, this);
}

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) {
    System.out.println("speed " + myLocation.getSpeed());

        //show location on map.................
        //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)).snippet("You are here!").title("You are here!"));
        //show distance............................

        if(startDistance == true)
        {

            Toast.makeText(MainActivity.this,
                      "Location has changed",    
                      Toast.LENGTH_LONG).show();
                if(myLocation != null)
                {
                    //latitude.setText("Current Latitude: " + String.valueOf(loc2.getLatitude())); 
                    //longitude.setText("Current Longitude: " + String.valueOf(loc2.getLongitude()));
                    float[] results = new float[1]; 
                    Location.distanceBetween(lat3, lon3, myLocation.getLatitude(), myLocation.getLongitude(), results);
                    System.out.println("Distance is: " + results[0]);               

                    dist += results[0];            
                    DecimalFormat df = new DecimalFormat("#.##"); // adjust this as appropriate
                if(count==1)
                {
                    distance.setText(df.format(dist) + "meters");
                }
                    lat3=myLocation.getLatitude();
                    lon3=myLocation.getLongitude();
                    count=1;
              }

        }
        if(startButtonClicked == true)
        {
            startDistance=true;
        }
}

@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();
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, this);
}
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;
    }
}}

0 个答案:

没有答案