[英]Google Map driving directions between two locations in my own application
I was working on Google maps, I need Google map driving direction between two locations(my current location and destination location) in my own application I don't want to open any google maps application. 我正在使用Google地图,我需要在自己的应用程序中的两个位置(我当前的位置和目的地位置)之间使用Google地图行车路线,但我不想打开任何Google Maps应用程序。 so please suggest me how to do this.
所以请建议我该怎么做。 up to now i have completed integrating google maps, zoom to my current location, placing a marker in destination lat-long.
到现在为止,我已经完成了Google地图的整合,缩放到当前位置,并在目标纬度位置放置了一个标记。
my java file: 我的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) {
}
} }
Check out this Awesome and easy to use Library: https://github.com/jd-alexander/Google-Directions-Android 看看这个很棒且易于使用的库: https : //github.com/jd-alexander/Google-Directions-Android
I don't know the end goal of your project. 我不知道您的项目的最终目标。 But if your intention is to get directions from point A to point B. Google's Roads Api can provide you with the data you need to draw the directions on a map or whatever you want.
但是,如果您要获取从A点到B点的路线,则Google的Roads Api可以为您提供所需的数据,以便在地图上或任何您想要的地方绘制路线。
It provides you the raw points even the speed in that particular road segment. 它为您提供原始点,甚至是该特定路段的速度。 It takes care of the redundant data that might lead you to draw directions through buildings for instance.
例如,它会处理多余的数据,这些数据可能会导致您绘制建筑物的方向。
Finally got the result: use this API link: http://maps.googleapis.com/maps/api/directions/json?origin=(origin)&destination=(destination ) 最终得到结果:使用此API链接: http : //maps.googleapis.com/maps/api/directions/json?origin=(origin )& destination=(destination )
where it will return JSON as a response which contains latitudes and longitudes, and store them in an array and draw polylines using those lat-long points in google maps 它将返回JSON作为包含纬度和经度的响应,并将其存储在数组中,并使用Google地图中的那些经纬度点绘制折线
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.