2013-08-23 58 views
1

Can I increase the accuracy of my starting position?我的marker always catchnear position point我的起點,而不是起點。如何提高我的起始位置的準確性?

以下是我的編碼:

@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); 

    } 

這是我的標誌功能的問題?任何人都可以幫我解決這個問題?

×××我的位置繪製路徑編碼×××

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) { 

     } 

    }; 

我可以做兩個固定點繪製路徑已經但是我希望做真正的時間散步,你走繪製的路徑。 Upper是我的onlocation更改繪製路徑代碼,但它有一些錯誤。它會把路程拉到一半,不會繼續吸引安格?任何人都可以幫助我或建議我更好的方法來做到這一點?

回答

0

的完全匹配的「藍點」的位置,使用

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

使用LocationManager不屈從OnMyLocationChangeListener得到相同的結果,因爲這是使用不同的供應商:"gps""network""fused"