2016-11-11 17 views
2

我在Google地图上工作,我需要在两个位置(我当前的位置和目的地位置)之间的Google地图驾驶方向我不想打开任何Google地图应用程序。所以请建议我如何做到这一点。到目前为止,我已经完成了整合谷歌地图,放大到我目前的位置,在目的地lat-long放置一个标记。谷歌地图在我自己的应用程序中的两个位置之间的行车路线

我的Java文件:

public class GoogleMapActivity extends FragmentActivity implements 
    OnMapReadyCallback, GoogleMap.OnMarkerClickListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { 

Location mLastLocation; 
GoogleApiClient mGoogleApiClient; 
private GoogleMap mMap; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_google_map); 

    SupportMapFragment mapFragment = 
      (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 
    Log.d("Tag", "in onc control in try"); 
    buildGoogleApiClient(); 
} 

protected void onStart() { 
    mGoogleApiClient.connect(); 
    super.onStart(); 
} 

protected void onStop() { 
    mGoogleApiClient.disconnect(); 
    super.onStop(); 
} 

protected synchronized void buildGoogleApiClient() { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 
    mGoogleApiClient.connect(); 
} 
@Override 
public void onConnected(@Nullable Bundle bundle) { 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
      mGoogleApiClient); 
    if (mLastLocation != null) { 
     Log.d("TAG","lat"+mLastLocation.getLatitude()); 
     Log.d("TAG","lng"+mLastLocation.getLongitude()); 
     ToastHelper.blueToast(getApplicationContext(), "location is " + mLastLocation.getLatitude() + " " + mLastLocation.getLongitude()); 
     mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude()), 12.0f)); 
     LatLng destination = new LatLng(14.880499, 79.988847); 
     mMap.addMarker(new MarkerOptions().position(destination).title("Destination")); 
    } 
    else { 
     Log.d("TAG","mLastLocation is null"); 
    } 
} 

@Override 
public void onConnectionSuspended(int i) { 
} 

/** 
* Called when the map is ready. 
*/ 
@Override 
public void onMapReady(GoogleMap map) { 
    mMap = map; 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    mMap.setMyLocationEnabled(true); 
} 
/** 
* Called when the user clicks a marker. 
*/ 
@Override 
public boolean onMarkerClick(final Marker marker) { 
    // Retrieve the data from the marker. 
    // Return false to indicate that we have not consumed the event and that we wish 
    // for the default behavior to occur (which is for the camera to move such that the 
    // marker is centered and for the marker's info window to open, if it has one). 
    return false; 
} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 

}

+0

你为什么选择这样做?这是可行的,但准备好自己做一些工作。 –

+0

感谢@Martian的建议,是他们的任何直接谷歌API获取两个位置之间的方向? –

回答

0

有用于由谷歌路线webservice的公共API。就我所知,它并未与android API捆绑在一起。

在这里,您与link去,这将让你开始

0

退房这个真棒和易于使用的库: https://github.com/jd-alexander/Google-Directions-Android

  • 这个库可以计算两个位置 之间的航线将其显示在地图上。
  • 它为您提供了一个路由点列表,您可以在Google地图中绘制路线以显示路线和路线。
0

我不知道您的项目的最终目标。但是,如果您的目的是从A点到B点获取路线,那么Google的Roads Api可以为您提供在地图上绘制路线或任何所需内容所需的数据。

它为您提供原始点,即使在特定路段的速度。它会处理可能导致您通过建筑物绘制方向的冗余数据。

相关问题