2014-10-10 125 views
1

我想創建一個顯示地圖的活動。當我點擊一個按鈕時,我將一個LatLng點保存到列表中。所以我走在街上,走路時,我點擊按鈕,我想在地圖上顯示我的路線。所以我嘗試寫下這段代碼但未找到:如何在地圖上顯示路線

public void settaMappa(View view){ 
     LocationResult locationResult = new LocationResult(){ 
      @Override 
      public void gotLocation(Location location){ 
       //Got the location! 
       System.out.println("ho avuto un segnale gps valido "); 
       mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map2)).getMap(); 
       if(listaPuntiTerreno == null) 
        listaPuntiTerreno = new ArrayList<LatLng>(); 
       if (location != null) 
       { 

        LatLng latLong = new LatLng(location.getLatitude(), location.getLongitude()); 
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLong, 13)); 

        CameraPosition cameraPosition = new CameraPosition.Builder() 
        .target(new LatLng(location.getLatitude(), location.getLongitude()))  // Sets the center of the map to location user 
        .zoom(17)     // Sets the zoom 
        .bearing(90)    // Sets the orientation of the camera to east 
        .tilt(40)     // Sets the tilt of the camera to 30 degrees 
        .build();     // Creates a CameraPosition from the builder 
        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
        listaPuntiTerreno.add(latLong); 
        disegnaTracciato(); 
       } 
      } 
     }; 
     MyLocation myLocation = new MyLocation(); 
     myLocation.getLocation(this, locationResult); 
    } 


    public void disegnaTracciato(){ 
     if(listaPuntiTerreno !=null && 
       listaPuntiTerreno.size()>1){ 

      mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map2)).getMap(); 

      PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true); 
      for (int z = 0; z < listaPuntiTerreno.size(); z++) { 
       LatLng point = listaPuntiTerreno.get(z); 
       options.add(point); 
      } 
      Polyline line = mMap.addPolyline(options); 
     } 
    } 

我們可以幫我嗎?

最佳reguards

+0

實際上你的問題是什麼? – 2014-10-10 09:42:49

+0

想要用您的特定路線動畫您的當前位置? – Piyush 2014-10-10 09:44:36

+0

問題是這樣的:當我把地圖PolylineOptions放入地圖時,我沒有在地圖上看到輪廓。 – bircastri 2014-10-10 10:10:12

回答

1

我已經固定我的問題,代碼是這樣的:

public MyLocation myLocation; 
    /** 
    * il metodo consente di registrare il punto 
    */ 
    LocationResult locationResult = new LocationResult(){ 
     @Override 
     public void gotLocation(Location location){ 
      //Got the location! 
      System.out.println("ho avuto un segnale gps valido "); 

      if (location != null) 
      { 
       LatLng latLong = new LatLng(location.getMyLocation().getLatitude(), 
         location.getMyLocation().getLongitude()); 
       mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLong, 25)); 
       listaPuntiTerreno.add(latLong); 
       Toast toast = Toast.makeText(getApplicationContext(), "Punto registrato correttamente", Toast.LENGTH_SHORT); 
       toast.show(); 
       disegnaTracciato(); 
      } 
     } 
    }; 

當我點擊按鈕,我把這個方法:

public void registraPunto(View view){ 
    myLocation.getLocation(this, locationResult); 
} 

,這是一類MyLocation(我從另一篇文章中複製了此課程)

public class MyLocation { 
    Timer timer1; 
    LocationManager lm; 
    LocationResult locationResult; 
    boolean gps_enabled=false; 
    boolean network_enabled=false; 
    private ProgressDialog pDialog; 
    private final static int LOCALIZATION_DELAY = 10000; 

    public boolean getLocation(Context context, LocationResult result) 
    { 
     //I use LocationResult callback class to pass location value from MyLocation to user code. 
     locationResult=result; 
     if(lm==null) 
      lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 

     //exceptions will be thrown if provider is not permitted. 
     try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){} 
     try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){} 

     //don't start listeners if no provider is enabled 
     if(!gps_enabled && !network_enabled) 
      return false; 

     if(gps_enabled) 
      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps); 
     if(network_enabled) 
      lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork); 

     pDialog = ProgressDialog.show(context, "Attendere ...", "Localizzazione in corso ...", true); 
     pDialog.setCancelable(false); 

     timer1=new Timer(); 
     timer1.schedule(new GetLastLocation(), LOCALIZATION_DELAY); 
     return true; 
    } 

    LocationListener locationListenerGps = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      timer1.cancel(); 
      if(pDialog.isShowing()) 
       pDialog.dismiss(); 
      locationResult.gotLocation(location); 
      lm.removeUpdates(this); 
      lm.removeUpdates(locationListenerNetwork); 
     } 
     public void onProviderDisabled(String provider) {} 
     public void onProviderEnabled(String provider) {} 
     public void onStatusChanged(String provider, int status, Bundle extras) {} 
    }; 

    LocationListener locationListenerNetwork = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      timer1.cancel(); 
      if(pDialog.isShowing()) 
       pDialog.dismiss(); 
      locationResult.gotLocation(location); 
      lm.removeUpdates(this); 
      lm.removeUpdates(locationListenerGps); 
     } 
     public void onProviderDisabled(String provider) {} 
     public void onProviderEnabled(String provider) {} 
     public void onStatusChanged(String provider, int status, Bundle extras) {} 
    }; 

    class GetLastLocation extends TimerTask { 
     @Override 
     public void run() { 
      if(pDialog.isShowing()) 
       pDialog.dismiss(); 

      lm.removeUpdates(locationListenerGps); 
      lm.removeUpdates(locationListenerNetwork); 

      Location net_loc=null, gps_loc=null; 
      if(gps_enabled) 
       gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
      if(network_enabled) 
       net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

      //if there are both values use the latest one 
      if(gps_loc!=null && net_loc!=null){ 
       if(gps_loc.getTime()>net_loc.getTime()) 
        locationResult.gotLocation(gps_loc); 
       else 
        locationResult.gotLocation(net_loc); 
       return; 
      } 

      if(gps_loc!=null){ 
       locationResult.gotLocation(gps_loc); 
       return; 
      } 

      if(net_loc!=null){ 
       locationResult.gotLocation(net_loc); 
       return; 
      } 

      locationResult.gotLocation(null); 
     } 
    } 

    public static abstract class LocationResult{ 
     public abstract void gotLocation(Location location); 
    } 
} 

現在,當我嘗試點擊一個按鈕時,我從MyLocation類獲得一個位置,但經度和緯度錯誤。如果我在maps.google.it上插入這個數字,我會發現我的真實位置是錯誤的,如果我從mMap對象獲得經度和緯度,這些都是正確的。

我的錯誤在哪裏? 我們可以幫助我嗎?