无法显示从当前位置到目的地谷歌地图 V2 的路径
Posted
技术标签:
【中文标题】无法显示从当前位置到目的地谷歌地图 V2 的路径【英文标题】:Not able to display path from current location to destination google maps V2 【发布时间】:2015-06-12 13:29:40 【问题描述】:我正在尝试使用答案android: How to draw route directions google maps API V2 from current location to destination 绘制从当前位置到目的地的路径。
但是我想使用我的真实位置来代替当前位置的硬编码 I 经度和纬度。但是每当我尝试这样做时,应用程序都会因 line location = mMap.getMyLocation(); 中的错误而崩溃;
感谢任何帮助。
MapsActivity.Java
public class MapsActivity extends FragmentActivity
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
Location location;
LatLng myPosition;
GMapV2Direction md;
LatLng fromPosition = getYourLocation();
LatLng toPosition = new LatLng(13.683660045847258, 100.53900808095932);
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
md = new GMapV2Direction();
setUpMapIfNeeded();
LatLng coordinates = new LatLng(13.685400079263206, 100.537133384495975);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 16));
mMap.addMarker(new MarkerOptions().position(fromPosition).title("Start"));
mMap.addMarker(new MarkerOptions().position(toPosition).title("End"));
Document doc = md.getDocument(fromPosition, toPosition, GMapV2Direction.MODE_DRIVING);
int duration = md.getDurationValue(doc);
String distance = md.getDistanceText(doc);
String start_address = md.getStartAddress(doc);
String copy_right = md.getCopyRights(doc);
ArrayList<LatLng> directionPoint = md.getDirection(doc);
PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);
for(int i = 0 ; i < directionPoint.size() ; i++)
rectLine.add(directionPoint.get(i));
mMap.addPolyline(rectLine);
@Override
protected void onResume()
super.onResume();
setUpMapIfNeeded();
/**
* Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
* installed) and the map has not already been instantiated.. This will ensure that we only ever
* <p/>
* If it isn't installed @link SupportMapFragment (and
* @link com.google.android.gms.maps.MapView MapView) will show a prompt for the user to
* install/update the Google Play services APK on their device.
* <p/>
* A user can return to this FragmentActivity after following the prompt and correctly
* installing/updating/enabling the Google Play services. Since the FragmentActivity may not
* have been completely destroyed during this process (it is likely that it would only be
* stopped or paused), @link #onCreate(Bundle) may not be called again so we should call this
* method in @link #onResume() to guarantee that it will be called.
*/
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)
/*myPosition = getLocation();
ZoomCurrentLocation(myPosition);*/
private LatLng getYourLocation()
mMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
double latitude = 0;
double longitude = 0;
if (location != null)
// Getting latitude of the current location
latitude = location.getLatitude();
// Getting longitude of the current location
longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
return latLng;
private void ZoomCurrentLocation(LatLng myPosition)
mMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myPosition, 16));
private void setUpMap(LatLng myPosition)
mMap.addMarker(new MarkerOptions().position(myPosition).title("Marker"));
【问题讨论】:
你的意思是location = mMap.getMyLocation();
是代码中getYourLocation()
的方法,crash在Location location = locationManager.getLastKnownLocation(provider);
?
按照给定的三个链接:wptrafficanalyzer.in/blog/…wptrafficanalyzer.in/blog/…wptrafficanalyzer.in/blog/…
【参考方案1】:
你的意思是location = mMap.getMyLocation();
是代码中getYourLocation()
的方法,crash在Location location = locationManager.getLastKnownLocation(provider);
吗?
如果是这样,可能是NullPointer Exception
引起的,你需要使用新的apiFusedLocationApi
来避免getLastLocation空指针。
请查看here 了解如何使用它。 here 是我 github 上的代码。
您可以查看here 了解目的地。
【讨论】:
感谢您的回答,但是有什么方法可以让我还可以找到这两个点之间的预计旅行时间和它们之间的距离? 好的,请查看here、here和here 为什么当我点击路线时它会打开谷歌地图?它不能显示该活动本身的路径吗?以上是关于无法显示从当前位置到目的地谷歌地图 V2 的路径的主要内容,如果未能解决你的问题,请参考以下文章
使用谷歌地图 API v2 和路线 API 的分步驾驶说明?