Java 在我自己的应用程序中谷歌地图在两个地点之间的驾驶方向
我在谷歌地图上工作,我需要在我自己的应用程序中两个位置(我的当前位置和目的地位置)之间的谷歌地图驾驶方向。我不想打开任何谷歌地图应用程序。所以请建议我怎么做。到目前为止,我已经完成整合谷歌地图,缩放到我的当前位置,在目的地lat long放置一个标记 我的java文件:Java 在我自己的应用程序中谷歌地图在两个地点之间的驾驶方向,java,android,google-maps,Java,Android,Google Maps,我在谷歌地图上工作,我需要在我自己的应用程序中两个位置(我的当前位置和目的地位置)之间的谷歌地图驾驶方向。我不想打开任何谷歌地图应用程序。所以请建议我怎么做。到目前为止,我已经完成整合谷歌地图,缩放到我的当前位置,在目的地lat long放置一个标记 我的java文件: public class GoogleMapActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMarkerClick
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) {
}
}有一个Web服务公共API,用于谷歌的方向指示。据我所知,它并没有与android API捆绑在一起
在这里,您将开始使用它。有一个用于谷歌方向的webservice公共API。据我所知,它并没有与android API捆绑在一起
在这里,您可以开始使用它查看这个很棒且易于使用的库:
- 此库允许您计算两个位置之间的路线 并将其显示在地图上
- 它为您提供了一个路由点列表,您可以在谷歌地图中绘制该列表以显示路由和方向
- 此库允许您计算两个位置之间的路线 并将其显示在地图上
- 它为您提供了一个路由点列表,您可以在谷歌地图中绘制该列表以显示路由和方向
- 我不知道你项目的最终目标。但是,如果你的目的是获取从A点到B点的方向,谷歌可以为你提供在地图上或任何你想要的地方绘制方向所需的数据
它为您提供原始点,甚至是特定路段的速度。例如,它会处理可能导致您绘制穿过建筑物的方向的冗余数据 我不知道你项目的最终目标。但是,如果你的目的是获取从A点到B点的方向,谷歌可以为你提供在地图上或任何你想要的地方绘制方向所需的数据 它为您提供原始点,甚至是特定路段的速度。例如,它会处理可能导致您绘制穿过建筑物的方向的冗余数据 最终得到了结果: 使用此API链接: ) 它将返回JSON作为包含纬度和经度的响应, 然后将它们存储在一个数组中,并使用谷歌地图中的那些lat-long点绘制多段线最终得到结果: 使用此API链接: ) 它将返回JSON作为包含纬度和经度的响应,
然后将它们存储在一个数组中,并使用谷歌地图中的那些lat-long点绘制多段线为什么选择这样做?这是可行的,但要准备好自己做大量的工作。感谢@Martian的建议,他们有没有直接的谷歌api来获取两个地点之间的方向?你为什么选择这样做?这是可行的,但要准备好自己做大量的工作。感谢@Martian的建议,他们有没有直接的谷歌api来获取两个地点之间的方向?