我正在使用 Google Map Markers 和 Polylines 为 Android 制作旅游 map ,我成功地做到了这一点,但现在我需要添加一些可以使我的应用程序对用户更友好的东西。所以我想把这个功能放到我的应用程序中。
类似的东西。
当用户移动时实时更新。
无论如何这是我的应用
我不知道如何开始放置方向。谁能帮帮我?
尝试过使用这种方法,但我失败了。
` @覆盖 public void onLocationChanged(位置位置){ //获取当前位置的纬度 双纬度 = 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"));
}
`
最佳答案
尝试使用此代码,您将在 map 上实时获得更新的位置。
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
} }
关于java - 使用 Google Map Api v2 的 Android onLocationChanged with Direction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34304279/