使用Google Map Api v2的Android onLocationChanged带有方向

3
我正在为安卓制作一张旅游地图,并使用Google Map标记和折线进行标注,目前已经成功实现。但是现在我需要添加一些可以使我的应用更加用户友好的功能,因此我想将这个功能加入到我的应用中。就像这样。

enter image description here

当用户移动时,实时更新。

无论如何,这是我的应用程序。

enter image description here

我不知道如何开始设置方向,有人能帮帮我吗?
我尝试使用这种方法但失败了。
``` @Override public void onLocationChanged(Location location) { // 获取当前位置的纬度 double latitude = 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"));
}

`


你能展示一下你的onLocationChanged代码吗?你是否考虑使用LocationManager? - bhanu.cs
@BhanuChowdary 问题已编辑 - Pseudorandom
那么当你调试时,onLocationChanged方法是否被执行了? - bhanu.cs
要确认是否成功....你是在模拟器上尝试吗?如果你是在模拟器上尝试的话,在模拟器控制中你需要更改纬度和经度以执行此功能。你也检查了GPS是否打开吗? - bhanu.cs
@BhanuChowdary 我使用的是移动版Zenfone手机,位置已启用。 - Pseudorandom
让我们在聊天中继续这个讨论 - Pseudorandom
2个回答

2

尝试这段代码,您将在地图上实时获取更新的位置。

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.lang.ClassCastException: com.sprikiwiki.tourist.touristmap.MainActivity无法转换为com.google.android.gms.location.LocationListeneronConnected方法LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, (com.google.android.gms.location.LocationListener) this); - Pseudorandom
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> - Prathap Badavath
当然,如果没有那个权限,我怎么能获取谷歌地图呢?嗯。 - Pseudorandom
在您的代码中,请勿强制转换此行((com.google.android.gms.location.LocationListener) this)。请查看我的代码。 - Prathap Badavath
让我们在聊天中继续这个讨论 - Pseudorandom

0

您需要在Google开发者控制台中启用Google地图方向API。要获取方向,您需要传递两个latlng作为源和目的地,并使用您的API密钥,您将获得给定源和目的地之间的所有点,绘制它们之间的折线,您将获得在屏幕截图中显示的源和目的地之间的方向折线,请参考此链接


1
这与问题有什么关系? - bhanu.cs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接