How to increase the accuracy of my start position?

2019-08-02 13:26发布

问题:

Can I increase the accuracy of my starting position? My marker always catch the near position point of my starting point and not the staring point.

Following is my coding:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria(); // Position Supporter Standard
        bestProvider = mgr.getBestProvider(criteria, true); // Choose the Best
                                                            // Positon
        Location location = mgr.getLastKnownLocation(bestProvider);
        latitude1 = gps.getLatitude(location);
        longitude1 = gps.getLongitude(location);

        // Getting reference to SupportMapFragment of the activity_main
        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);

        // Getting Map for the SupportMapFragment
        map = fm.getMap();

        // Enable MyLocation Button in the Map
        map.setMyLocationEnabled(true);

    } // OnCreate

    // start from second page
    protected void onStart() {
        super.onStart();
        loadMap();
    }

    // Marker first point
    private void loadMap() {
        // latitude1 = gps.getLatitude(location);
        // longitude1 = gps.getLongitude(location);

        LatLng position = new LatLng(latitude1, longitude1);
        Toast.makeText(getApplicationContext(),
                "LOADlat1:" + latitude1 + " / " + "LOADlog1:" + longitude1,
                Toast.LENGTH_LONG).show();

        // Instantiating MarkerOptions class
        MarkerOptions options = new MarkerOptions();

        // Setting position for the MarkerOptions
        options.position(position);

        // add marker
        map.addMarker(options);
        Log.i("Running+", "Options3: " + String.valueOf(position));
        Log.i("Running+", "Options3: " + String.valueOf(options));

        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);

        map = fm.getMap();

        map.setMyLocationEnabled(true);

    }

It is my marker function problem? Anyone can help me to fix this?

×××My On Location Draw Path Code×××

LocationListener onLocationChange = new LocationListener(){ 
     @Override
     public void onLocationChanged(Location location) {  
         if (location != null) {
            Toast.makeText(getApplicationContext(), "Location NO EQUAL NULL AND START!", Toast.LENGTH_LONG).show();

            GPSTracker1 gps2;
            gps2 = new GPSTracker1(MainActivity2.this);

            latitude2 = gps2.getLatitude();
            longitude2 = gps2.getLongitude();                   

                str_origin = "origin="+ latitude1 +","+ longitude1;
            //Log.i("Running+", "Origin Status: " + str_origin);

            // Destination of route
            String str_dest2 = "destination="+ latitude2 +","+ longitude2;      
            //Log.i("LOCATION+", "Destination Status: " + latitude2 + ", " + longitude2);



            // Sensor enabled
            String sensor = "sensor=false";         

            // Building the parameters to the web service
            String parameters = str_origin+"&"+str_dest2+"&"+sensor;


                    // Output format
                    String output = "json";


                    // Building the url to the web service
                    String url2 = "https://maps.googleapis.com/maps/api/directions/"+output+"?"+parameters;
                    //Log.i("Running+", "Map Get Location: " + url);
                    DownloadTask downloadTask2 = new DownloadTask();

                    downloadTask2.execute(url2);                                    


                Toast.makeText(getApplicationContext(), "Location Change 123!!", Toast.LENGTH_LONG).show();
                Toast.makeText(getApplicationContext(), "lat1:" + latitude1 + " / " + "log1:" + longitude1, Toast.LENGTH_LONG).show();
                Toast.makeText(getApplicationContext(), "lat2:" + latitude2 + " / " + "log2:" + longitude2, Toast.LENGTH_LONG).show();

                // Getting reference to SupportMapFragment of the activity_main
                SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);

                // Getting Map for the SupportMapFragment
                map = fm.getMap();

                // Enable MyLocation Button in the Map
                map.setMyLocationEnabled(true);     

                Toast.makeText(getApplicationContext(), "Location Change Map!!", Toast.LENGTH_LONG).show();
         }


        }  



        public void onProviderDisabled(String arg0) {  

        }  
        public void onProviderEnabled(String arg0) {  

        }  
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 

        }

    };

I can do draw path from two fix point already but I want do the real time walk and draw the path of you walk. Upper is my onlocation change draw path code, but it have some bug. It will draw the path to half and will no continue draw agian? Can anybody help me or suggest me more better method to do this?

回答1:

The exactly match the "blue dot" position, use

map.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
    ...
});

Using LocationManager will never yield the same result you get from OnMyLocationChangeListener, because this it is using a different provider: "gps" or "network" vs. "fused".