2015-10-17 54 views
0

我已經搜索了近10個小時這個東西,但沒有運氣的互聯網...關於如何在谷歌地圖V2使用谷歌方向API的乾淨和清晰的解釋將真的有幫助....如何在android中使用庫實現Google Directions API?

我走了通過整個漫長的過程,但我想用一個庫,因爲我懶得實施那個龐大的代碼與我的...

我已經成功地從polok實施這個庫。

但你可以在截圖是方向不準確,多段線幾圈後動越野....

我也tyczj發現這個庫看看。

但沒有實現....

使用這個庫將是非常有益或者實現谷歌direcions API任何其他方式的例子.....

這裏是我的MapActivity的片段:

public class MapActivity extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, LocationListener { 

    private GoogleMap mMap; // Might be null if Google Play services APK is not available 
    double latitude, longitude; 
    Bundle extras; 
    String add; 
    Geocoder geocoder; 
    LatLng position; 

    private Location mLastLocation; 

    // Google client to interact with Google API 
    private GoogleApiClient mGoogleApiClient; 
    private boolean mInProgress; 


    // boolean flag to toggle periodic location updates 

    private LocationRequest mLocationRequest; 

    // Location updates intervals in sec 
    private static int UPDATE_INTERVAL = 10000; // 10 sec 
    private static int FATEST_INTERVAL = 5000; // 5 sec 
    private static int DISPLACEMENT = 1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_map); 
     setUpMapIfNeeded(); 
     extras = getIntent().getExtras(); 
     mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
     mMap.setMyLocationEnabled(true); 

     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 

     mLocationRequest = LocationRequest.create() 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
       .setInterval(UPDATE_INTERVAL) 
       .setFastestInterval(FATEST_INTERVAL) 
       .setSmallestDisplacement(DISPLACEMENT); 

     if (!mGoogleApiClient.isConnected() || !mGoogleApiClient.isConnecting() && !mInProgress) { 
      mInProgress = true; 
      mGoogleApiClient.connect(); 
     } 


     add = extras.getString("address"); 

     geocoder = new Geocoder(this); 

     try { 
      List<Address> addresses = geocoder.getFromLocationName(add,1); 

      if(addresses.size()>0){ 

       latitude = addresses.get(0).getLatitude(); 
       longitude = addresses.get(0).getLongitude(); 

      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     position = new LatLng(latitude,longitude); 

     Marker marker = mMap.addMarker(new MarkerOptions().position(position).title(add)); 

     CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(marker.getPosition(), 12F); 

     mMap.animateCamera(cu); 


    } 

    @Override 
    protected void onDestroy() { 

     mInProgress = false; 
     if (mGoogleApiClient.isConnected()) { 
      LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
      mGoogleApiClient.disconnect(); 
     } 

     super.onDestroy(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     setUpMapIfNeeded(); 
    } 

    private void setUpMapIfNeeded() { 
     // Do a null check to confirm that we have not already instantiated the map. 
     if (mMap == null) { 
      // Try to obtain the map from the SupportMapFragment. 
      mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
        .getMap(); 

      // Check if we were successful in obtaining the map. 
      if (mMap != null) { 

      } 
     } 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 

     LocationServices.FusedLocationApi.requestLocationUpdates(
       mGoogleApiClient, mLocationRequest, this); 

     displayLocation(); 


    } 

    @Override 
    public void onConnectionSuspended(int i) { 

     mGoogleApiClient.connect(); 

    } 

    @Override 
    public void onLocationChanged(Location location) { 

     mLastLocation = location; 

     displayLocation(); 


    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 

    } 

    private LatLng displayLocation() { 

     mLastLocation = LocationServices.FusedLocationApi 
       .getLastLocation(mGoogleApiClient); 

     if (mLastLocation != null) { 
      double myLatitude = mLastLocation.getLatitude(); 
      double myLongitude = mLastLocation.getLongitude(); 

      mMap.addMarker(new MarkerOptions().position(new LatLng(myLatitude, myLongitude)) 
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.current)) 
        .title("Me")); 

      return new LatLng(myLatitude,myLongitude); 

     } 

     return null; 
    } 
} 

謝謝大家的回答.....

回答

0

我發現我自己.....

對不起,這不是一個很好的問題,雖然後來太多,如果其他人正在尋找類似的答案這裏是一個:

我發現this庫實行以來,檢查這個一個...簡單精確實現路由...

相關問題