Android onLocationChanged with Direction using Google Map Api v2
Android onLocationChanged with Direction using Google Map Api v2
我正在使用 Google 地图标记和折线为 android 制作旅游地图,我成功地做到了这一点,但现在我需要添加一些可以使我的应用程序对用户更友好的东西。所以我想把这个功能放到我的应用程序中。
类似的东西。
用户移动时实时更新。
无论如何这是我的应用程序
我不知道如何开始放置方向。谁能帮帮我?
尝试过使用此方法但我失败了。
`
@覆盖
public void onLocationChanged(Location 位置) {
//获取当前位置的纬度
双纬度 = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(18));
start = latLng;
Log.d("Location Changed: ", latLng.toString());
//etLog.append("Location Changed: " + latLng.toString() + System.getProperty("line.separator"));
}
`
您必须从 google 开发者控制台启用 google 地图方向 api。要获取路线,您必须使用 api 键传递两个经纬度作为源和目的地,您将获得给定源和目的地之间的所有点,在它们之间绘制折线,您将获得在源和目的地之间绘制的方向折线,如中所示截图关注 this link.
试试这个代码,您将在地图上实时获得更新的位置。
public class MapActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,LocationListener{
final String TAG = "mapactivity";
LocationRequest locationRequest;
GoogleAPiClient googleApiClient;
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
@Override
public void onStart(){
super.onStart();
googleApiClient.connect();
}
@Override
public void onStop(){
googleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnectionSuspended(int i){
Log.i(TAG, "Connection suspended");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult){
Log.i(TAG, "connection failed");
}
@Override
public void onConnected(Bundle bundle){
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(1000);
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
@Override
public void onLocationChanged(Location location){
Double curLat = location.getLatitude();//current latitude
Double curLong = location.getLongitude();//current longitude
} }
我正在使用 Google 地图标记和折线为 android 制作旅游地图,我成功地做到了这一点,但现在我需要添加一些可以使我的应用程序对用户更友好的东西。所以我想把这个功能放到我的应用程序中。
类似的东西。
用户移动时实时更新。
无论如何这是我的应用程序
我不知道如何开始放置方向。谁能帮帮我?
尝试过使用此方法但我失败了。
` @覆盖 public void onLocationChanged(Location 位置) { //获取当前位置的纬度 双纬度 = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(18));
start = latLng;
Log.d("Location Changed: ", latLng.toString());
//etLog.append("Location Changed: " + latLng.toString() + System.getProperty("line.separator"));
}
`
您必须从 google 开发者控制台启用 google 地图方向 api。要获取路线,您必须使用 api 键传递两个经纬度作为源和目的地,您将获得给定源和目的地之间的所有点,在它们之间绘制折线,您将获得在源和目的地之间绘制的方向折线,如中所示截图关注 this link.
试试这个代码,您将在地图上实时获得更新的位置。
public class MapActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,LocationListener{
final String TAG = "mapactivity";
LocationRequest locationRequest;
GoogleAPiClient googleApiClient;
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
@Override
public void onStart(){
super.onStart();
googleApiClient.connect();
}
@Override
public void onStop(){
googleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnectionSuspended(int i){
Log.i(TAG, "Connection suspended");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult){
Log.i(TAG, "connection failed");
}
@Override
public void onConnected(Bundle bundle){
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(1000);
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
@Override
public void onLocationChanged(Location location){
Double curLat = location.getLatitude();//current latitude
Double curLong = location.getLongitude();//current longitude
} }