コード例 #1
0
 /// <summary>
 /// 清空导航控件(初始化)
 /// </summary>
 public void Clear()
 {
     flpRoutes.Controls.Clear();
     if (BMapControl != null)
     {
         BMapControl.SetRoute(null); //
         BMapControl.SetRouteStartAndEnd(null, null);
     }
 }
コード例 #2
0
        /// <summary>
        /// 开启搜索
        /// </summary>
        internal void StartSearch()
        {
            flpRoutes.Controls.Clear();
            if (BMapControl != null)
            {
                BMapControl.SetRoute(null); //
                BMapControl.SetRouteStartAndEnd(null, null);
            }
            if (BPlacesBoard != null) //位置列表初始化
            {
                BPlacesBoard.Clear();
            }
            _wait.Visible = true; //等待
            ((Action) delegate()
            {
                JObject routes;
                DirectionV1Service ds = new DirectionV1Service();
                if (_current_method == 0) //公交
                {
                    if (bPlaceBoxSource.City != "" && bPlaceBoxDestination.City != "" && bPlaceBoxSource.City != bPlaceBoxDestination.City)
                    {
                        MessageBox.Show("公交导航时,两地城市必须一致!");
                        return;
                    }
                    routes = ds.DirectionByTransit(bPlaceBoxSource.QueryText, bPlaceBoxDestination.QueryText, _currentCity);
                }
                else if (_current_method == 1) //驾车
                {
                    routes = ds.DirectionByDriving(bPlaceBoxSource.QueryText, bPlaceBoxDestination.QueryText, bPlaceBoxSource.City == "" ? bPlaceBoxSource.CurrentCity : bPlaceBoxSource.City
                                                   , bPlaceBoxDestination.City == "" ? bPlaceBoxDestination.CurrentCity : bPlaceBoxDestination.City);
                }
                else //步行
                {
                    if (bPlaceBoxSource.City != "" && bPlaceBoxDestination.City != "" && bPlaceBoxSource.City != bPlaceBoxDestination.City)
                    {
                        MessageBox.Show("步行导航时,两地城市必须一致!");
                        return;
                    }
                    routes = ds.DirectionByWalking(bPlaceBoxSource.QueryText, bPlaceBoxDestination.QueryText, _currentCity);
                }
                if (routes != null && (string)routes["status"] == "0") //搜索成功
                {
                    this.Invoke((Action) delegate()
                    {
                        _wait.Visible = false;
                        if ((string)routes["type"] == "2") //正常结果
                        {
                            //生成起点终点
                            BPoint start = new BPoint {
                                Type = PointType.RouteStart, Selected = false, Address = bPlaceBoxSource.QueryText, Location = new LatLngPoint(double.Parse((string)routes["result"]["origin"]["originPt"]["lng"]), double.Parse((string)routes["result"]["origin"]["originPt"]["lat"]))
                            };
                            BPoint end = new BPoint {
                                Type = PointType.RouteEnd, Selected = false, Address = bPlaceBoxDestination.QueryText, Location = new LatLngPoint(double.Parse((string)routes["result"]["destination"]["destinationPt"]["lng"]), double.Parse((string)routes["result"]["destination"]["destinationPt"]["lat"]))
                            };
                            if (BMapControl != null)
                            {
                                BMapControl.SetRouteStartAndEnd(start, end); //设置地图中对应的起点 终点
                            }
                            if (_current_method == 0)                        //公交
                            {
                                BTaxiTipControl taxi = new BTaxiTipControl();
                                taxi.DataSource = routes["result"]["taxi"]; //打车信息
                                taxi.Width = flpRoutes.Width - 25;
                                flpRoutes.Controls.Add(taxi);
                                foreach (JObject route in routes["result"]["routes"])
                                {
                                    BTransitRouteItem item = new BTransitRouteItem();

                                    BStepStartAndEndItem origin = new BStepStartAndEndItem();
                                    origin.EndPoint = start;
                                    item.Origin = origin; //起点

                                    BStepStartAndEndItem destination = new BStepStartAndEndItem();
                                    destination.EndPoint = end;
                                    item.Destination = destination; //终点

                                    item.DataSource = new BRoute {
                                        Type = RouteType.Transit, DataSource = route
                                    };
                                    flpRoutes.Controls.Add(item);
                                    item.Width = flpRoutes.Width - 25;

                                    item.StepEndPointSelected += new StepEndPointSelectedEventHandler(item_StepEndPointSelected);
                                    item.StepSelected += new StepSelectedEventHandler(item_StepSelected);
                                    item.RouteSelected += new RouteSelectedEventHandler(item_RouteSelected);
                                }
                            }
                            else if (_current_method == 1) //驾车
                            {
                                BTaxiTipControl taxi = new BTaxiTipControl();
                                taxi.DataSource = routes["result"]["taxi"]; //打车信息
                                taxi.Width = flpRoutes.Width - 25;
                                flpRoutes.Controls.Add(taxi);
                                foreach (JObject route in routes["result"]["routes"])
                                {
                                    BDrivingRouteItem item = new BDrivingRouteItem();

                                    BStepStartAndEndItem origin = new BStepStartAndEndItem();
                                    origin.EndPoint = start;
                                    item.Origin = origin; //起点

                                    BStepStartAndEndItem destination = new BStepStartAndEndItem();
                                    destination.EndPoint = end;
                                    item.Destination = destination; //终点

                                    item.DataSource = new BRoute {
                                        DataSource = route, Type = RouteType.Driving
                                    };
                                    flpRoutes.Controls.Add(item);
                                    item.Width = flpRoutes.Width - 25;

                                    item.StepEndPointSelected += new StepEndPointSelectedEventHandler(item_StepEndPointSelected);
                                    item.StepSelected += new StepSelectedEventHandler(item_StepSelected);
                                    item.RouteSelected += new RouteSelectedEventHandler(item_RouteSelected);
                                }
                            }
                            else  //步行
                            {
                                //步行没有打车信息
                                foreach (JObject route in routes["result"]["routes"])
                                {
                                    BWalkingRouteItem item = new BWalkingRouteItem();

                                    BStepStartAndEndItem origin = new BStepStartAndEndItem();
                                    origin.EndPoint = start;
                                    item.Origin = origin; //起点

                                    BStepStartAndEndItem destination = new BStepStartAndEndItem();
                                    destination.EndPoint = end;
                                    item.Destination = destination; //终点

                                    item.DataSource = new BRoute {
                                        DataSource = route, Type = RouteType.Walking
                                    };
                                    flpRoutes.Controls.Add(item);
                                    item.Width = flpRoutes.Width - 25;

                                    item.StepEndPointSelected += new StepEndPointSelectedEventHandler(item_StepEndPointSelected);
                                    item.StepSelected += new StepSelectedEventHandler(item_StepSelected);
                                    item.RouteSelected += new RouteSelectedEventHandler(item_RouteSelected);
                                }
                            }
                        }
                        else //地址模糊 需重新选择  具体json格式参见api文档
                        {
                            //
                            string start_keyword = (string)routes["result"]["originInfo"]["wd"];
                            string end_keyword = (string)routes["result"]["destinationInfo"]["wd"];
                            Label l = new Label();
                            l.AutoSize = false;
                            l.ForeColor = Color.Red;
                            l.Width = flpRoutes.Width - 25; l.Height = 20;
                            l.TextAlign = ContentAlignment.MiddleCenter;
                            l.Text = "请选择准确的位置";
                            flpRoutes.Controls.Add(l);
                            if (routes["result"]["origin"] != null)       //起点模糊
                            {
                                if (routes["result"]["origin"] is JArray) //公交
                                {
                                    BPlacesSuggestionControl sugg_start = new BPlacesSuggestionControl();
                                    sugg_start.Type = PointType.RouteStart;
                                    sugg_start.KeyWord = start_keyword;
                                    sugg_start.Content = routes["result"]["origin"];
                                    sugg_start.Width = flpRoutes.Width - 25;
                                    sugg_start.EndPointSelected += new EndPointSelectedEventHandler(sugg_start_EndPointSelected);
                                    flpRoutes.Controls.Add(sugg_start);
                                }
                                else //驾车 步行
                                {
                                    BPlacesSuggestionControl sugg_start = new BPlacesSuggestionControl();
                                    sugg_start.Type = PointType.RouteStart;
                                    sugg_start.KeyWord = start_keyword;
                                    sugg_start.Content = routes["result"]["origin"]["content"];
                                    sugg_start.Width = flpRoutes.Width - 25;
                                    sugg_start.EndPointSelected += new EndPointSelectedEventHandler(sugg_start_EndPointSelected);
                                    flpRoutes.Controls.Add(sugg_start);
                                }
                            }
                            if (routes["result"]["destination"] != null)       //终点模糊
                            {
                                if (routes["result"]["destination"] is JArray) //公交
                                {
                                    BPlacesSuggestionControl sugg_end = new BPlacesSuggestionControl();
                                    sugg_end.Type = PointType.RouteEnd;
                                    sugg_end.KeyWord = end_keyword;
                                    sugg_end.Content = routes["result"]["destination"];
                                    sugg_end.Width = flpRoutes.Width - 25;
                                    sugg_end.EndPointSelected += new EndPointSelectedEventHandler(sugg_end_EndPointSelected);
                                    flpRoutes.Controls.Add(sugg_end);
                                }
                                else
                                {
                                    BPlacesSuggestionControl sugg_end = new BPlacesSuggestionControl();
                                    sugg_end.Type = PointType.RouteEnd;
                                    sugg_end.KeyWord = end_keyword;
                                    sugg_end.Content = routes["result"]["destination"]["content"];
                                    sugg_end.Width = flpRoutes.Width - 25;
                                    sugg_end.EndPointSelected += new EndPointSelectedEventHandler(sugg_end_EndPointSelected);
                                    flpRoutes.Controls.Add(sugg_end);
                                }
                            }
                        }
                    });
                }
            }).BeginInvoke(null, null);
        }