我已经在互联网上搜索了近10个小时这个东西,但没有运气......关于如何在Google地图v2中使用google指令api的清晰明确的解释真的很有用....
我已经经历了整个漫长的过程,但我想使用一个库,因为我太懒了,无法用我的那个实现那么大的代码......
我已成功从polok实施此库。
但正如您在屏幕截图中看到的那样,方向不准确,折线在几个转弯后越野移动....
我还通过tyczj找到了这个库。
但没有实施......
使用此库的示例将非常有用或以任何其他方式实现Google direcions API .....
以下是我的MapActivity的片段:
public class MapActivity extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener {
private GoogleMap mMap; // Might be null if Google Play services APK is not available
double latitude, longitude;
Bundle extras;
String add;
Geocoder geocoder;
LatLng position;
private Location mLastLocation;
// Google client to interact with Google API
private GoogleApiClient mGoogleApiClient;
private boolean mInProgress;
// boolean flag to toggle periodic location updates
private LocationRequest mLocationRequest;
// Location updates intervals in sec
private static int UPDATE_INTERVAL = 10000; // 10 sec
private static int FATEST_INTERVAL = 5000; // 5 sec
private static int DISPLACEMENT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
setUpMapIfNeeded();
extras = getIntent().getExtras();
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.setMyLocationEnabled(true);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(UPDATE_INTERVAL)
.setFastestInterval(FATEST_INTERVAL)
.setSmallestDisplacement(DISPLACEMENT);
if (!mGoogleApiClient.isConnected() || !mGoogleApiClient.isConnecting() && !mInProgress) {
mInProgress = true;
mGoogleApiClient.connect();
}
add = extras.getString("address");
geocoder = new Geocoder(this);
try {
List<Address> addresses = geocoder.getFromLocationName(add,1);
if(addresses.size()>0){
latitude = addresses.get(0).getLatitude();
longitude = addresses.get(0).getLongitude();
}
} catch (IOException e) {
e.printStackTrace();
}
position = new LatLng(latitude,longitude);
Marker marker = mMap.addMarker(new MarkerOptions().position(position).title(add));
CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(marker.getPosition(), 12F);
mMap.animateCamera(cu);
}
@Override
protected void onDestroy() {
mInProgress = false;
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
super.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
}
}
}
@Override
public void onConnected(Bundle bundle) {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
displayLocation();
}
@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
displayLocation();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
private LatLng displayLocation() {
mLastLocation = LocationServices.FusedLocationApi
.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
double myLatitude = mLastLocation.getLatitude();
double myLongitude = mLastLocation.getLongitude();
mMap.addMarker(new MarkerOptions().position(new LatLng(myLatitude, myLongitude))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.current))
.title("Me"));
return new LatLng(myLatitude,myLongitude);
}
return null;
}
}
谢谢大家的回复......
答案 0 :(得分:0)