// 点击查看总数按钮 private void buttonTotal_Click(object sender, EventArgs e) { // 关键字 string keyWord = getKeyWord(); if (string.IsNullOrEmpty(keyWord)) { MessageBox.Show("请输入自定义关键字"); return; } // 开始请求 Region region = comboBoxCity.SelectedItem as Region; handler = new PlaceAPIHandler(region, keyWord); int totalNum = handler.GetTotalNum((comboBoxProvince.SelectedItem as Region).Name); if (totalNum < 0) // 返回错误 { MessageBox.Show(PlaceAPIHandler.GetErrMsg(totalNum)); } else // 有结果 { MessageBox.Show(string.Format("区域:{0},关键词:{1},共有 {2} 个结果", region.Name, keyWord, totalNum)); } }
// 点击分块建议按钮 private void buttonBlock_Click(object sender, EventArgs e) { // 关键字 string keyWord = getKeyWord(); if (string.IsNullOrEmpty(keyWord)) { MessageBox.Show("请输入自定义关键字"); return; } // 获取对应矢量 string cityName = comboBoxCity.SelectedItem.ToString(); City city = CityDao.SelectByName(cityName); if (city == null) { MessageBox.Show("此城市没有相应的矢量数据,无法分块获取POI"); return; } int fid = Convert.ToInt32(city.Id); // 开始请求 Region region = comboBoxCity.SelectedItem as Region; handler = new PlaceAPIHandler(region, keyWord); int totalNum = handler.GetTotalNum((comboBoxProvince.SelectedItem as Region).Name); if (totalNum < 0) // 返回错误 { MessageBox.Show(PlaceAPIHandler.GetErrMsg(totalNum)); } else if (totalNum <= 400) { MessageBox.Show(string.Format("区域:{0},关键词:{1},共有 {2} 个结果,无需分块", region.Name, keyWord, totalNum)); checkBoxBlock.Checked = false; } else // 有结果 { int blockNum = totalNum / 400 + 1; // 最少分块数 Envelope envelope = GisUtil.GetEnvelope(fid); double x = envelope.MaxX - envelope.MinX; double y = envelope.MaxY - envelope.MinY; int xNum = 1; int yNum = 1; if (x <= y) { double r = y / x; xNum = Convert.ToInt32(Math.Sqrt(blockNum / r)) + 1; yNum = Convert.ToInt32(xNum * r) + 1; } else { double r = x / y; yNum = Convert.ToInt32(Math.Sqrt(blockNum / r)) + 1; xNum = Convert.ToInt32(yNum * r) + 1; } xNum *= 2; yNum *= 2; MessageBox.Show(string.Format("区域:{0},关键词:{1},共有 {2} 个结果。建议分块为 {3} 行,{4} 列", region.Name, keyWord, totalNum, yNum, xNum)); // 自动调整行列分块数 if (xNum > 20) { comboBoxBlockCol.Items.Clear(); for (int i = 1; i <= xNum + 10; i++) { comboBoxBlockCol.Items.Add(i); } } comboBoxBlockCol.SelectedIndex = xNum - 1; if (yNum > 20) { comboBoxBlockRow.Items.Clear(); for (int j = 1; j <= yNum + 10; j++) { comboBoxBlockRow.Items.Add(j); } } comboBoxBlockRow.SelectedIndex = yNum - 1; } }
// 点击开始按钮 private void buttonStart_Click(object sender, EventArgs e) { // 初始化 dataGridView1.Rows.Clear(); toolStripStatusLabelResult.Text = ""; // 关键字 string keyWord = getKeyWord(); if (string.IsNullOrEmpty(keyWord)) { MessageBox.Show("请输入自定义关键字"); return; } // excel导出路径 IWorkbook workbook = null; ISheet sheet = null; if (checkBoxExport.Checked) { if (string.IsNullOrEmpty(textBoxSave.Text.Trim())) { MessageBox.Show("请选择数据导出路径"); return; } // 创建 excel workbook = new XSSFWorkbook(); sheet = workbook.CreateSheet("poi"); XlsUtil.AddBaiduHeader(sheet); } // 开始请求 toolStripStatusLabelStatus.Text = "请求开始……"; // 获取总数 handler = new PlaceAPIHandler(comboBoxCity.SelectedItem as Region, keyWord, dataGridView1, sheet); int totalNum = handler.GetTotalNum((comboBoxProvince.SelectedItem as Region).Name); if (totalNum < 0) // 返回错误 { string errMsg = PlaceAPIHandler.GetErrMsg(totalNum); MessageBox.Show(errMsg); toolStripStatusLabelStatus.Text = "就绪"; toolStripStatusLabelResult.Text = errMsg; return; } else if (totalNum == 0) { string msg = "没有搜索结果"; MessageBox.Show(msg); toolStripStatusLabelStatus.Text = "就绪"; toolStripStatusLabelResult.Text = msg; return; } else // 有结果,开始请求 { int rowNum = 1; int colNum = 1; if (totalNum > 400 && checkBoxBlock.Checked) // 分块获取 { rowNum = Convert.ToInt32(comboBoxBlockRow.SelectedItem); colNum = Convert.ToInt32(comboBoxBlockCol.SelectedItem); if (rowNum * colNum > 150) // 可能超过配额 { if (MessageBox.Show("请求可能超过配额,是否继续?", "", MessageBoxButtons.OKCancel) == DialogResult.Cancel) // 取消请求 { toolStripStatusLabelStatus.Text = "就绪"; return; } } } // 请求 requestData(rowNum, colNum); // 保存 excel if (workbook != null) { FileStream fs = File.Create(textBoxSave.Text); workbook.Write(fs); workbook.Close(); fs.Close(); } } }