简体   繁体   English

Android onLocationChanged with Direction使用Google Map Api v2

[英]Android onLocationChanged with Direction using Google Map Api v2

I am making a tourist map for android with Google Map Markers and Polylines and I succeeded in doing that but now i need to add something that can make my app more friendly user. 我正在使用谷歌地图标记和折线制作一个Android的旅游地图,我成功地做到了,但现在我需要添加一些可以使我的应用程序更友好的用户。 so I wanted to put this feature to my app. 所以我想把这个功能放到我的应用程序中。

Something like that. 这样的事情。

在此输入图像描述

with live update when the user move. 用户移动时进行实时更新。

Anyhow this my app 总之这是我的应用程序

在此输入图像描述

I don't know how to start on that placing DIRECTION. 我不知道如何开始放置DIRECTION。 anyone can help me? 有人可以帮帮我吗?

Tried using this method but i failed. 尝试使用这种方法,但我失败了。

` @Override public void onLocationChanged(Location location) { // Getting latitude of the current location double latitude = location.getLatitude(); `@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"));
}

` `

Try this code you will get updated location live on 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
} }

You have to enable google map directions api from google developer console. 您必须从谷歌开发者控制台启用谷歌地图方向api。 To get directions you have to pass two latlngs as source and destination with your api key you will get all the points between given source and destination draw the polyline between them you will get directions polyline drawn between source and destination as shown in screenshot follow this link. 要获得方向,你必须通过两项latlngs源和目标与你的API密钥,您将得到所有给定的源和目的地之间的点,画,你会得到方向折线源和目的地之间绘制他们之间的折线,如图截图遵循这个链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM