public MKRoute NewRoute(MKMapItem start, MKMapItem finish) { MKDirectionsRequest drivingRouteRequest = new MKDirectionsRequest(); drivingRouteRequest.TransportType = MKDirectionsTransportType.Automobile; drivingRouteRequest.Source = start; drivingRouteRequest.Destination = finish; MKRoute drivingRoute = null; MKDirections drivingRouteDirections = new MKDirections(drivingRouteRequest); drivingRouteDirections.CalculateDirections((drivingRouteResponse, drivingRouteError) => { if (drivingRouteError != null) { drivingRoute = null; } else { // The code doesn't request alternate routes, so add the single calculated route to // a previously declared MKRoute property called walkingRoute. drivingRoute = drivingRouteResponse.Routes[0]; } }); return(drivingRoute); var rteLine = new MKPolylineRenderer(drivingRoute.Polyline) { LineWidth = 5.0f, StrokeColor = UIColor.Purple }; mapView.GetRendererForOverlay = (mv, ol) => rteLine; mapView.AddOverlay(route.Polyline, MKOverlayLevel.AboveRoads); }
MapView mMapView = null; // 地图View protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); /** * 使用地图sdk前需先初始化BMapManager. * BMapManager是全局的,可为多个MapView共用,它需要地图模块创建前创建, * 并在地图地图模块销毁后销毁,只要还有地图模块在使用,BMapManager就不应该销毁 */ DemoApplication app = (DemoApplication)this.Application; if (app.mBMapManager == null) { app.mBMapManager = new BMapManager(ApplicationContext); /** * 如果BMapManager没有初始化则初始化BMapManager */ app.mBMapManager.Init(new DemoApplication.MyGeneralListener()); } SetContentView(Resource.Layout.activity_customroute); ICharSequence titleLable = new String("路线规划功能——自设路线示例"); Title = titleLable.ToString(); //初始化地图 mMapView = FindViewById<MapView>(Resource.Id.bmapView); mMapView.Controller.EnableClick(true); mMapView.Controller.SetZoom(13); /** 演示自定义路线使用方法 * 在北京地图上画一个北斗七星 * 想知道某个点的百度经纬度坐标请点击:http://api.map.baidu.com/lbsapi/getpoint/index.html */ GeoPoint p1 = new GeoPoint((int)(39.9411 * 1E6), (int)(116.3714 * 1E6)); GeoPoint p2 = new GeoPoint((int)(39.9498 * 1E6), (int)(116.3785 * 1E6)); GeoPoint p3 = new GeoPoint((int)(39.9436 * 1E6), (int)(116.4029 * 1E6)); GeoPoint p4 = new GeoPoint((int)(39.9329 * 1E6), (int)(116.4035 * 1E6)); GeoPoint p5 = new GeoPoint((int)(39.9218 * 1E6), (int)(116.4115 * 1E6)); GeoPoint p6 = new GeoPoint((int)(39.9144 * 1E6), (int)(116.4230 * 1E6)); GeoPoint p7 = new GeoPoint((int)(39.9126 * 1E6), (int)(116.4387 * 1E6)); //起点坐标 GeoPoint start = p1; //终点坐标 GeoPoint stop = p7; //第一站,站点坐标为p3,经过p1,p2 GeoPoint[] step1 = new GeoPoint[3]; step1[0] = p1; step1[1] = p2; step1[2] = p3; //第二站,站点坐标为p5,经过p4 GeoPoint[] step2 = new GeoPoint[2]; step2[0] = p4; step2[1] = p5; //第三站,站点坐标为p7,经过p6 GeoPoint[] step3 = new GeoPoint[2]; step3[0] = p6; step3[1] = p7; //站点数据保存在一个二维数据中 GeoPoint[][] routeData = new GeoPoint[3][]; routeData[0] = step1; routeData[1] = step2; routeData[2] = step3; //用站点数据构建一个MKRoute MKRoute route = new MKRoute(); route.CustomizeRoute(start, stop, routeData); //将包含站点信息的MKRoute添加到RouteOverlay中 RouteOverlay routeOverlay = new RouteOverlay(this, mMapView); routeOverlay.SetData(route); //向地图添加构造好的RouteOverlay mMapView.Overlays.Add(routeOverlay); //执行刷新使生效 mMapView.Refresh(); }
/** * 发起路线规划搜索示例 * @param v */ void SearchButtonProcess(View v) { //重置浏览节点的路线数据 route = null; routeOverlay = null; transitOverlay = null; mBtnPre.Visibility = ViewStates.Invisible; mBtnNext.Visibility = ViewStates.Invisible; // 处理搜索按钮响应 EditText editSt = FindViewById<EditText>(Resource.Id.start); EditText editEn = FindViewById<EditText>(Resource.Id.end); // 对起点终点的name进行赋值,也可以直接对坐标赋值,赋值坐标则将根据坐标进行搜索 MKPlanNode stNode = new MKPlanNode(); stNode.Name = editSt.Text; MKPlanNode enNode = new MKPlanNode(); enNode.Name = editEn.Text; // 实际使用中请对起点终点城市进行正确的设定 if (mBtnDrive.Equals(v)) { mSearch.DrivingSearch("北京", stNode, "北京", enNode); } else if (mBtnTransit.Equals(v)) { mSearch.TransitSearch("北京", stNode, enNode); } else if (mBtnWalk.Equals(v)) { mSearch.WalkingSearch("北京", stNode, "北京", enNode); } }
/// <summary> /// Sets the route data /// </summary> /// <param name="route">PCL route</param> /// <param name="nativeRoute">Native route</param> private void SetRouteData(TKRoute route, MKRoute nativeRoute) { var routeFunctions = (IRouteFunctions)route; var steps = new TKRouteStep[nativeRoute.Steps.Count()]; for (int i = 0; i < steps.Length; i++) { steps[i] = new TKRouteStep(); var stepFunction = (IRouteStepFunctions)steps[i]; var nativeStep = nativeRoute.Steps.ElementAt(i); stepFunction.SetInstructions(nativeStep.Instructions); stepFunction.SetDistance(nativeStep.Distance); } routeFunctions.SetSteps(steps); routeFunctions.SetDistance(nativeRoute.Distance); routeFunctions.SetTravelTime(nativeRoute.ExpectedTravelTime); routeFunctions.SetBounds( MapSpan.FromCenterAndRadius( new Position( nativeRoute.Polyline.BoundingMapRect.MidX, nativeRoute.Polyline.BoundingMapRect.MidY), Distance.FromKilometers( route.Source.DistanceTo(route.Destination)))); }