public static PlaceSuggestionWrp Suggest(string query, string region) { PlaceSuggestionService pSSv = new PlaceSuggestionService(); JObject jObj = pSSv.Suggestion(query, region); PlaceSuggestionWrp rslt = jObj.ToObject <PlaceSuggestionWrp>(); return(rslt); }
private void button1_Click(object sender, EventArgs e) { PlaceSuggestionService pSSv = new PlaceSuggestionService(); JObject jObj = pSSv.Suggestion("WeWork", "上海"); PlaceSuggestionWrp rslt = jObj.ToObject <PlaceSuggestionWrp>(); LocationLL loc = rslt.result[1].location; double diffLat = LocationBase.Get1KMLat(); double diffLng = LocationBase.Get1KMLng(loc.lat); LocationTravelData data = new LocationTravelData(); TravelFullData fullData = new TravelFullData(); StringBuilder sb = new StringBuilder(); { LocationLL eastOceanLoc = new LocationLL(121.487487, 31.236564); LocationLL yunnanLoc = new LocationLL(121.487264, 31.233405); loc = yunnanLoc; } int range = 10; double gap = 0.5; for (int i = -range; i < range; i++) { for (int j = -range; j < range; j++) { if (i == 0 && j == 0) { continue; } try { LocationLL loc2 = loc.Clone <LocationLL>(); loc2.lat += diffLat * i * gap; loc2.lng += diffLng * j * gap; DirectionV1Wrp dirV1 = DirectionV1Wrp.DirectionByTransit(loc, loc2, "上海"); LocationTravelData.Data dataItem = new LocationTravelData.Data(); dataItem.Loc = loc2; dataItem.Distance = dirV1.result.routes[0].scheme[0].distance; dataItem.Duration = dirV1.result.routes[0].scheme[0].duration; data.DataList.Add(dataItem); GeoDecodingServiceWrp deCdWrp = GeoDecodingServiceWrp.GeoDecoding(loc2.Clone <LocationXY>()); sb.AppendLine(deCdWrp.result.addressComponent.street + deCdWrp.result.addressComponent.street_number + "; " + dataItem.Distance.ToString() + "; " + dataItem.Duration.ToString() + "//r"); TravelFullData.Data dataItemB = new TravelFullData.Data(); dataItemB.Target = loc2; dataItemB.TargetAddress = deCdWrp.result.addressComponent; dataItemB.OneTravelScheme = dirV1.result.routes[0].scheme[0]; fullData.DataList.Add(dataItemB); } catch { } } rTxBx.Text = sb.ToString(); } JsonSerializer serializer = new JsonSerializer(); serializer.NullValueHandling = NullValueHandling.Ignore; using (StreamWriter sw0 = new StreamWriter(@"C:\dev\baidu\json0.txt")) using (StreamWriter sw1 = new StreamWriter(@"C:\dev\baidu\json1.txt")) using (JsonWriter writer0 = new JsonTextWriter(sw0)) using (JsonWriter writer1 = new JsonTextWriter(sw1)) { serializer.Serialize(writer0, fullData); serializer.Serialize(writer1, data); } }
/// <summary> /// 输入框发生变化 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void txtInput_TextChanged(object sender, EventArgs e) { if (txtInput.Text != "") { if (!_search) { _search = true; return; } _district = ""; _city = ""; ((Action)(delegate() //异步调用API 获取建议位置 { PlaceSuggestionService pss = new PlaceSuggestionService(); JObject suggestion_places = pss.Suggestion(txtInput.Text, CurrentCity); //建议位置 if (suggestion_places != null) { this.Invoke((Action) delegate() { _suggestion_places.Controls.Clear(); _suggestion_places.Padding = new System.Windows.Forms.Padding(3); _suggestion_places.Width = Width; _suggestion_places.BackColor = Color.White; _suggestion_places.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; foreach (JObject place in suggestion_places["result"]) //返回JSON结构请参见百度API文档 { Label lbl = new Label(); lbl.MouseEnter += new EventHandler(lbl_MouseEnter); lbl.MouseLeave += new EventHandler(lbl_MouseLeave); lbl.Click += new EventHandler(lbl_Click); lbl.BackColor = Color.White; lbl.AutoSize = false; lbl.Padding = new System.Windows.Forms.Padding(0, 5, 0, 5); lbl.Font = new System.Drawing.Font("微软雅黑", 9); lbl.Width = _suggestion_places.Width - 15; lbl.Height = 30; lbl.TextAlign = ContentAlignment.MiddleLeft; lbl.Image = Properties.BMap.ico_search; lbl.ImageAlign = ContentAlignment.MiddleLeft; lbl.Tag = (string)place["name"] + "|" + (string)place["district"] + "|" + (string)place["city"]; lbl.Text = " " + (string)place["name"] + " " + (string)place["city"] + "-" + (string)place["district"]; //返回JSON结构请参见百度API文档 _suggestion_places.Controls.Add(lbl); } if (_suggestion_places.Controls.Count > 0) { Control top = FindTheTopControl(); if (top != null) { Point p = new Point(txtInput.Left - 1, txtInput.Top + txtInput.Height); //文本框在BPlaceBox中的位置 Point location = top.PointToClient(PointToScreen(p)); //文本框位置转换为最顶层控件坐标系中对应位置 _suggestion_places.Location = location; _suggestion_places.Height = _suggestion_places.Controls.Count * _suggestion_places.Controls[0].Height + 10; if (!top.Controls.Contains(_suggestion_places)) { top.Controls.Add(_suggestion_places); } _suggestion_places.Visible = true; _suggestion_places.BringToFront(); } } else { _suggestion_places.Visible = false; } }); } })).BeginInvoke(null, null); } else { _suggestion_places.Visible = false; } }