首页 文章

Android onLocationChanged with Direction使用Google Map Api v2

提问于
浏览
3

我正在使用谷歌 Map 标记和折线制作一个Android的旅游 Map ,我成功地做到了,但现在我需要添加一些可以使我的应用程序更友好的用户 . 所以我想把这个功能放到我的应用程序中 .

这样的事情 .

enter image description here

用户移动时进行实时更新 .

总之这是我的应用程序

enter image description here

我不知道如何开始放置DIRECTION . 有人可以帮帮我吗?

尝试使用这种方法,但我失败了 .

`@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"));
}

`

2 回答

  • 0

    试试这个代码,您将在 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
    } }
    
  • 2

    您必须从谷歌开发者控制台启用谷歌 Map 方向api . 要获得路线,您必须使用api键传递两个latlng作为源和目标,您将获得给定源和目标之间的所有点在它们之间绘制折线,您将获得在源和目标之间绘制的折线,如屏幕截图所示this link.

相关问题