可是我用调用的stLatLng和enLatLng去百度地图官网查的时候是有路径的,不知道为什么自己做的就不行,求帮助
public class MapFragment extends Fragment{
private LocationClient mLocationClient;
private MyLocationListener mLocationListener;
private BitmapDescriptor mIconLocation;
private BitmapDescriptor mMarker;
private RelativeLayout mMarkerLy;
private boolean isFirstIn = true;
private double mLatitude;
private double mLongitude;
private BaiduMap baiduMap;
private com.baidu.mapapi.map.MyLocationConfiguration.LocationMode mLocationMode;
MapView mMapView = null;
private static String LOCATION = "";
private MyOrientationListener myOrientationListener;
private float mCurrentX;
private LatLng enLatLng;
RouteLine route = null;
private RoutePlanSearch mSearch;// 路径规划搜索接口
private int drivintResultIndex = 0; //驾车路线方案index
private int totalLine = 0; //总路线数量
protected static final String Tag = "MyTag";
View mView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstancemap) {
SDKInitializer.initialize(getActivity().getApplicationContext());
super.onCreateView(inflater, container, savedInstancemap);
View mapLayout = inflater.inflate(R.layout.home_map_tag, container,
false);
mMapView = (MapView) mapLayout.findViewById(R.id.bmapview);
initview();
//实现定位
initlocation();
initMarker();
mMarkerLy = (RelativeLayout) mapLayout.findViewById(R.id.rl_maker_ly);
baiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {
@SuppressLint("NewApi")
@Override
public boolean onMarkerClick(Marker marker) {
Bundle extraInfo = marker.getExtraInfo();
MapDataInfo info = (MapDataInfo) extraInfo.getSerializable("info");
System.out.println(info.getName());
TextView distance = (TextView) mMarkerLy.findViewById(R.id.tv_info_distance);
TextView price = (TextView) mMarkerLy.findViewById(R.id.tv_info_price);
TextView name = (TextView) mMarkerLy.findViewById(R.id.tv_info_name);
TextView lastnum = (TextView) mMarkerLy.findViewById(R.id.tv_info_lastnum);
distance.setText("距离:" + info.getDistance() + "米");
price.setText("价格:" + String.valueOf(info.getParkingprice()) + "元/小时");
name.setText(info.getName());
lastnum.setText("剩余车位:" + String.valueOf(info.getLastparknum()));
enLatLng = new LatLng(info.getLatitude(), info.getLongitude());
System.out.println("This latlng : " + enLatLng);
InfoWindow infoWindow;
TextView tv = new TextView(getActivity());
tv.setBackgroundResource(R.drawable.location_tips);
tv.setPadding(30, 20, 30, 50);
tv.setTextColor(Color.parseColor("#ffffff"));
tv.setText(info.getName());
final LatLng latLng = marker.getPosition();
Point p = baiduMap.getProjection().toScreenLocation(latLng);
p.y -= 47;
LatLng ll = baiduMap.getProjection().fromScreenLocation(p);
BitmapDescriptor tvBD = BitmapDescriptorFactory.fromView(tv);
infoWindow = new InfoWindow(tvBD, ll, 0, new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick() {
baiduMap.hideInfoWindow();
}
});
baiduMap.showInfoWindow(infoWindow);
mMarkerLy.setVisibility(View.VISIBLE);
return true;
}
});
baiduMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public boolean onMapPoiClick(MapPoi arg0) {
return false;
}
@Override
public void onMapClick(LatLng arg0) {
mMarkerLy.setVisibility(View.GONE);
baiduMap.hideInfoWindow();
}
});
TextView navi = (TextView) mMarkerLy.findViewById(R.id.tv_navi_click);
navi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LatLng stLatLng = new LatLng(mLongitude,mLatitude);
System.out.println("start latlng : " + stLatLng);
navigation_click(stLatLng,enLatLng);
}
});
return mapLayout;
}
private void navigation_click(LatLng stLatLng,LatLng enLatLng){
mSearch = RoutePlanSearch.newInstance();
mSearch.setOnGetRoutePlanResultListener(listener);
DrivingRoutePlanOption drivingRoutePlanOption = new DrivingRoutePlanOption();
drivingRoutePlanOption.policy(DrivingPolicy.ECAR_TIME_FIRST);
drivingRoutePlanOption.from(PlanNode.withLocation(stLatLng));
drivingRoutePlanOption.to(PlanNode.withLocation(enLatLng));
mSearch.drivingSearch(drivingRoutePlanOption);
}
/**
* 路线规划结果回调
*/
OnGetRoutePlanResultListener listener = new OnGetRoutePlanResultListener() {
@Override
public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {
// 获取步行路线规划结果
}
@Override
public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {
// 获取公交换乘路径规划结果
}
@Override
public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {
// 获取驾车线路规划结果
//baiduMap.clear();
if(drivingRouteResult == null || drivingRouteResult.error != SearchResult.ERRORNO.NO_ERROR){
Toast.makeText(getActivity(), "抱歉,未找到结果!", Toast.LENGTH_SHORT).show();
}
if (drivingRouteResult.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
// 起终点或途经点地址有岐义,通过以下接口获取建议查询信息
//drivingRouteResult.getSuggestAddrInfo();
return;
}
if (drivingRouteResult.error == SearchResult.ERRORNO.NO_ERROR){
DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(baiduMap);
// baiduMap.setOnMarkerClickListener();
drivingRouteOverlay.setData(drivingRouteResult.getRouteLines().get(drivintResultIndex));
drivingRouteOverlay.addToMap();
drivingRouteOverlay.zoomToSpan();
totalLine = drivingRouteResult.getRouteLines().size();
Toast.makeText(getActivity(), "总路程:" + drivingRouteResult.getTaxiInfo().getDistance(), 1000).show();
}
}
};
private void initMarker() {
mMarker = BitmapDescriptorFactory.fromResource(R.drawable.maker);
addOverlays(MapDataInfo.infos);
}
private void addOverlays(List<MapDataInfo> infos) {
baiduMap.clear();
LatLng latLng = null;
Marker marker = null;
OverlayOptions options;
for(MapDataInfo info : infos){
latLng = new LatLng(info.getLongitude(),info.getLatitude());
options = new MarkerOptions().position(latLng).icon(mMarker).zIndex(5);
marker = (Marker) baiduMap.addOverlay(options);
Bundle bundle = new Bundle();
bundle.putSerializable("info", info);
marker.setExtraInfo(bundle);
}
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
baiduMap.setMapStatus(msu);
}
private void initview() {
baiduMap = mMapView.getMap();
baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
baiduMap.setTrafficEnabled(true);
MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);
baiduMap.setMapStatus(msu);
}
private void initlocation() {
mLocationMode = com.baidu.mapapi.map.MyLocationConfiguration.LocationMode.NORMAL;
mLocationClient = new LocationClient(getActivity());
mLocationListener = new MyLocationListener();
mLocationClient.registerLocationListener(mLocationListener);
LocationClientOption option = new LocationClientOption();
option.setCoorType("bd09ll"); //bd09ll
option.setIsNeedAddress(true); //允许获取地址
option.setOpenGps(true); //开启GPS
option.setScanSpan(1000); //定位间隔时间1s
mLocationClient.setLocOption(option);
mIconLocation = BitmapDescriptorFactory
.fromResource(R.drawable.bullet_blue);
myOrientationListener = new MyOrientationListener(this.getActivity().getApplicationContext());
myOrientationListener.setOnOrientationListener(new OnOrientationListener() {
@Override
public void onOrientationChanged(float x) {
mCurrentX = x;
}
});
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ImageButton imageButton = (ImageButton) getActivity().findViewById(R.id.ib_mylocation);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
local_to_mLocation();
}
});
}
//定位到我的位置
private void local_to_mLocation(){
LatLng latLng = new LatLng(mLatitude, mLongitude);
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
baiduMap.animateMapStatus(msu);
}
private class MyLocationListener implements BDLocationListener{
@Override
public void onReceiveLocation(BDLocation location) {
MyLocationData data = new MyLocationData.Builder()//
.accuracy(location.getRadius())//
.latitude(location.getLatitude())//
.longitude(location.getLongitude())//
.build();
baiduMap.setMyLocationData(data);
//设置自定义图标
MyLocationConfiguration configuration = new MyLocationConfiguration(
com.baidu.mapapi.map.MyLocationConfiguration.LocationMode.NORMAL, true, mIconLocation);
baiduMap.setMyLocationConfigeration(configuration);
//更新位置
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
//把定位所得的位置存储到LOCATION
LOCATION = Double.toString(mLongitude) + "," + Double.toString(mLatitude);
//第一次进入
if (isFirstIn) {
LatLng latLng = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
baiduMap.animateMapStatus(msu);
isFirstIn = false;
Toast.makeText(getActivity().getApplicationContext(), location.getAddrStr(),Toast.LENGTH_SHORT).show();
}
}
}
}
public class MapFragment extends Fragment{
private LocationClient mLocationClient;
private MyLocationListener mLocationListener;
private BitmapDescriptor mIconLocation;
private BitmapDescriptor mMarker;
private RelativeLayout mMarkerLy;
private boolean isFirstIn = true;
private double mLatitude;
private double mLongitude;
private BaiduMap baiduMap;
private com.baidu.mapapi.map.MyLocationConfiguration.LocationMode mLocationMode;
MapView mMapView = null;
private static String LOCATION = "";
private MyOrientationListener myOrientationListener;
private float mCurrentX;
private LatLng enLatLng;
RouteLine route = null;
private RoutePlanSearch mSearch;// 路径规划搜索接口
private int drivintResultIndex = 0; //驾车路线方案index
private int totalLine = 0; //总路线数量
protected static final String Tag = "MyTag";
View mView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstancemap) {
SDKInitializer.initialize(getActivity().getApplicationContext());
super.onCreateView(inflater, container, savedInstancemap);
View mapLayout = inflater.inflate(R.layout.home_map_tag, container,
false);
mMapView = (MapView) mapLayout.findViewById(R.id.bmapview);
initview();
//实现定位
initlocation();
initMarker();
mMarkerLy = (RelativeLayout) mapLayout.findViewById(R.id.rl_maker_ly);
baiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {
@SuppressLint("NewApi")
@Override
public boolean onMarkerClick(Marker marker) {
Bundle extraInfo = marker.getExtraInfo();
MapDataInfo info = (MapDataInfo) extraInfo.getSerializable("info");
System.out.println(info.getName());
TextView distance = (TextView) mMarkerLy.findViewById(R.id.tv_info_distance);
TextView price = (TextView) mMarkerLy.findViewById(R.id.tv_info_price);
TextView name = (TextView) mMarkerLy.findViewById(R.id.tv_info_name);
TextView lastnum = (TextView) mMarkerLy.findViewById(R.id.tv_info_lastnum);
distance.setText("距离:" + info.getDistance() + "米");
price.setText("价格:" + String.valueOf(info.getParkingprice()) + "元/小时");
name.setText(info.getName());
lastnum.setText("剩余车位:" + String.valueOf(info.getLastparknum()));
enLatLng = new LatLng(info.getLatitude(), info.getLongitude());
System.out.println("This latlng : " + enLatLng);
InfoWindow infoWindow;
TextView tv = new TextView(getActivity());
tv.setBackgroundResource(R.drawable.location_tips);
tv.setPadding(30, 20, 30, 50);
tv.setTextColor(Color.parseColor("#ffffff"));
tv.setText(info.getName());
final LatLng latLng = marker.getPosition();
Point p = baiduMap.getProjection().toScreenLocation(latLng);
p.y -= 47;
LatLng ll = baiduMap.getProjection().fromScreenLocation(p);
BitmapDescriptor tvBD = BitmapDescriptorFactory.fromView(tv);
infoWindow = new InfoWindow(tvBD, ll, 0, new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick() {
baiduMap.hideInfoWindow();
}
});
baiduMap.showInfoWindow(infoWindow);
mMarkerLy.setVisibility(View.VISIBLE);
return true;
}
});
baiduMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public boolean onMapPoiClick(MapPoi arg0) {
return false;
}
@Override
public void onMapClick(LatLng arg0) {
mMarkerLy.setVisibility(View.GONE);
baiduMap.hideInfoWindow();
}
});
TextView navi = (TextView) mMarkerLy.findViewById(R.id.tv_navi_click);
navi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LatLng stLatLng = new LatLng(mLongitude,mLatitude);
System.out.println("start latlng : " + stLatLng);
navigation_click(stLatLng,enLatLng);
}
});
return mapLayout;
}
private void navigation_click(LatLng stLatLng,LatLng enLatLng){
mSearch = RoutePlanSearch.newInstance();
mSearch.setOnGetRoutePlanResultListener(listener);
DrivingRoutePlanOption drivingRoutePlanOption = new DrivingRoutePlanOption();
drivingRoutePlanOption.policy(DrivingPolicy.ECAR_TIME_FIRST);
drivingRoutePlanOption.from(PlanNode.withLocation(stLatLng));
drivingRoutePlanOption.to(PlanNode.withLocation(enLatLng));
mSearch.drivingSearch(drivingRoutePlanOption);
}
/**
* 路线规划结果回调
*/
OnGetRoutePlanResultListener listener = new OnGetRoutePlanResultListener() {
@Override
public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {
// 获取步行路线规划结果
}
@Override
public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {
// 获取公交换乘路径规划结果
}
@Override
public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {
// 获取驾车线路规划结果
//baiduMap.clear();
if(drivingRouteResult == null || drivingRouteResult.error != SearchResult.ERRORNO.NO_ERROR){
Toast.makeText(getActivity(), "抱歉,未找到结果!", Toast.LENGTH_SHORT).show();
}
if (drivingRouteResult.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
// 起终点或途经点地址有岐义,通过以下接口获取建议查询信息
//drivingRouteResult.getSuggestAddrInfo();
return;
}
if (drivingRouteResult.error == SearchResult.ERRORNO.NO_ERROR){
DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(baiduMap);
// baiduMap.setOnMarkerClickListener();
drivingRouteOverlay.setData(drivingRouteResult.getRouteLines().get(drivintResultIndex));
drivingRouteOverlay.addToMap();
drivingRouteOverlay.zoomToSpan();
totalLine = drivingRouteResult.getRouteLines().size();
Toast.makeText(getActivity(), "总路程:" + drivingRouteResult.getTaxiInfo().getDistance(), 1000).show();
}
}
};
private void initMarker() {
mMarker = BitmapDescriptorFactory.fromResource(R.drawable.maker);
addOverlays(MapDataInfo.infos);
}
private void addOverlays(List<MapDataInfo> infos) {
baiduMap.clear();
LatLng latLng = null;
Marker marker = null;
OverlayOptions options;
for(MapDataInfo info : infos){
latLng = new LatLng(info.getLongitude(),info.getLatitude());
options = new MarkerOptions().position(latLng).icon(mMarker).zIndex(5);
marker = (Marker) baiduMap.addOverlay(options);
Bundle bundle = new Bundle();
bundle.putSerializable("info", info);
marker.setExtraInfo(bundle);
}
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
baiduMap.setMapStatus(msu);
}
private void initview() {
baiduMap = mMapView.getMap();
baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
baiduMap.setTrafficEnabled(true);
MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);
baiduMap.setMapStatus(msu);
}
private void initlocation() {
mLocationMode = com.baidu.mapapi.map.MyLocationConfiguration.LocationMode.NORMAL;
mLocationClient = new LocationClient(getActivity());
mLocationListener = new MyLocationListener();
mLocationClient.registerLocationListener(mLocationListener);
LocationClientOption option = new LocationClientOption();
option.setCoorType("bd09ll"); //bd09ll
option.setIsNeedAddress(true); //允许获取地址
option.setOpenGps(true); //开启GPS
option.setScanSpan(1000); //定位间隔时间1s
mLocationClient.setLocOption(option);
mIconLocation = BitmapDescriptorFactory
.fromResource(R.drawable.bullet_blue);
myOrientationListener = new MyOrientationListener(this.getActivity().getApplicationContext());
myOrientationListener.setOnOrientationListener(new OnOrientationListener() {
@Override
public void onOrientationChanged(float x) {
mCurrentX = x;
}
});
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ImageButton imageButton = (ImageButton) getActivity().findViewById(R.id.ib_mylocation);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
local_to_mLocation();
}
});
}
//定位到我的位置
private void local_to_mLocation(){
LatLng latLng = new LatLng(mLatitude, mLongitude);
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
baiduMap.animateMapStatus(msu);
}
private class MyLocationListener implements BDLocationListener{
@Override
public void onReceiveLocation(BDLocation location) {
MyLocationData data = new MyLocationData.Builder()//
.accuracy(location.getRadius())//
.latitude(location.getLatitude())//
.longitude(location.getLongitude())//
.build();
baiduMap.setMyLocationData(data);
//设置自定义图标
MyLocationConfiguration configuration = new MyLocationConfiguration(
com.baidu.mapapi.map.MyLocationConfiguration.LocationMode.NORMAL, true, mIconLocation);
baiduMap.setMyLocationConfigeration(configuration);
//更新位置
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
//把定位所得的位置存储到LOCATION
LOCATION = Double.toString(mLongitude) + "," + Double.toString(mLatitude);
//第一次进入
if (isFirstIn) {
LatLng latLng = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
baiduMap.animateMapStatus(msu);
isFirstIn = false;
Toast.makeText(getActivity().getApplicationContext(), location.getAddrStr(),Toast.LENGTH_SHORT).show();
}
}
}
}