using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
using CCWin;
using Newtonsoft.Json;
namespace BaiduDowload
{
public partial class Form1 : CCSkinMain
{
private readonly double a = 6378245.0;
private readonly double ee = 0.00669342162296594323;
private readonly double PI = 3.1415926535897932384626;
private double x_PI = 3.14159265358979324*3000.0/180.0;
public Form1()
{
InitializeComponent();
}
private void btn_dowload_Click(object sender, EventArgs e)
{
if (tbx_query.Text != "")
{
if (tbx_queryCity.Text != "")
{
if (path_text.Text != "")
{
var wc = new WebClient();
var fs = new FileStream(path_text.Text + "/" + tbx_query.Text + ".txt", FileMode.Append,
FileAccess.Write);
var sr = new StreamWriter(fs);
var city = tbx_queryCity.Text;
var totalnum = 20;
for (var i = 0; i < totalnum; i++)
{
var i_url = "http://api.map.baidu.com/place/v2/search?q=" + tbx_query.Text + "®ion=" +
city + "&scope=1&output=json&ak=9**************q&page_size=20&page_num=" +
i + "&coord_type=1&ret_coordtype=gcj02ll";
var i_bResponse = wc.DownloadData(i_url);
var i_strResponse = Encoding.UTF8.GetString(i_bResponse);
try
{
var schoolInfoList = JsonConvert.DeserializeObject<Info>(i_strResponse);
var resultcount = schoolInfoList.Results.Length;
for (var j = 0; j < resultcount; j++)
{
var mylng = Convert.ToDouble(schoolInfoList.Results[j].Location.Lng);
var mylat = Convert.ToDouble(schoolInfoList.Results[j].Location.Lat);
gcj02_wgs84(ref mylng, ref mylat);
var result = j + 1 + ". " + schoolInfoList.Results[j].Name + ":" + mylng + "," +
mylat + "\n";
sr.WriteLine(result);
rtb_result.Text += result;
rtb_result.SelectionStart = rtb_result.TextLength;
rtb_result.ScrollToCaret();
}
}
catch (Exception ex)
{
Console.Write(ex);
rtb_result.Text += i + 1 + "\n";
sr.WriteLine(i + 1);
rtb_result.SelectionStart = rtb_result.TextLength;
rtb_result.ScrollToCaret();
}
}
sr.Close();
fs.Close();
}
else
{
MessageBox.Show("資料夾路徑不能為空!");
}
}
else
{
MessageBox.Show("查詢城市不能為空!");
}
}
else
{
MessageBox.Show("查詢內容不能為空!");
}
}
#region GCJ02轉WGS84
private double transformlat(double lng, double lat)
{
double ret;
ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.Sqrt(Math.Abs(lng));
ret += (20.0 * Math.Sin(6.0 * lng * PI) + 20.0 * Math.Sin(2.0 * lng * PI)) * 2.0 / 3.0;
ret += (20.0 * Math.Sin(lat * PI) + 40.0 * Math.Sin(lat / 3.0 * PI)) * 2.0 / 3.0;
ret += (160.0 * Math.Sin(lat / 12.0 * PI) + 320 * Math.Sin(lat * PI / 30.0)) * 2.0 / 3.0;
return ret;
}
private double transformlng(double lng, double lat)
{
double ret;
ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.Sqrt(Math.Abs(lng));
ret += (20.0 * Math.Sin(6.0 * lng * PI) + 20.0 * Math.Sin(2.0 * lng * PI)) * 2.0 / 3.0;
ret += (20.0 * Math.Sin(lng * PI) + 40.0 * Math.Sin(lng / 3.0 * PI)) * 2.0 / 3.0;
ret += (150.0 * Math.Sin(lng / 12.0 * PI) + 300.0 * Math.Sin(lng / 30.0 * PI)) * 2.0 / 3.0;
return ret;
}
private void gcj02_wgs84(ref double lng, ref double lat)
{
var dlat = transformlat(lng - 105.0, lat - 35.0);
var dlng = transformlng(lng - 105.0, lat - 35.0);
var radlat = lat / 180.0 * PI;
var magic = Math.Sin(radlat);
magic = 1 - ee * magic * magic;
var sqrtmagic = Math.Sqrt(magic);
dlat = dlat * 180.0 / (a * (1 - ee) / (magic * sqrtmagic) * PI);
dlng = dlng * 180.0 / (a / sqrtmagic * Math.Cos(radlat) * PI);
var mglat = lat + dlat;
var mglng = lng + dlng;
lng = lng * 2 - mglng;
lat = lat * 2 - mglat;
}
#endregion
private void writeFile(string s)
{
var fs = new FileStream(path_text.Text + "/" + tbx_query.Text + ".txt", FileMode.Append, FileAccess.Write);
var sr = new StreamWriter(fs);
sr.WriteLine(s);
sr.Close();
fs.Close();
}
private void btn_file_Click(object sender, EventArgs e)
{
var mFolderBrowserDialog = new FolderBrowserDialog();
if (mFolderBrowserDialog.ShowDialog() == DialogResult.OK)
{
path_text.Text = mFolderBrowserDialog.SelectedPath;
}
}
private void rtb_result_TextChanged(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void 幫助ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("先設定儲存結果的資料夾,再點選下載按鈕", "幫助");
}
private void 關於ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("作者:Wu\n版權所有", "測試");
}
private void 設定儲存資料夾ToolStripMenuItem_Click(object sender, EventArgs e)
{
var mFolderBrowserDialog = new FolderBrowserDialog();
if (mFolderBrowserDialog.ShowDialog() == DialogResult.OK)
{
path_text.Text = mFolderBrowserDialog.SelectedPath;
}
}
}
#region 實體類
public class Info
{
[JsonProperty("status")]
public int Status { get; set; }
[JsonProperty("message")]
public string Message { get; set; }
[JsonProperty("total")]
public int Total { get; set; }
[JsonProperty("results")]
public Result[] Results { get; set; }
}
public class Result
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("location")]
public Location Location { get; set; }
[JsonProperty("address")]
public string Address { get; set; }
[JsonProperty("province")]
public string Province { get; set; }
[JsonProperty("city")]
public string City { get; set; }
[JsonProperty("area")]
public string Area { get; set; }
[JsonProperty("street_id")]
public string StreetId { get; set; }
[JsonProperty("telephone")]
public string Telephone { get; set; }
[JsonProperty("detail")]
public int Detail { get; set; }
[JsonProperty("uid")]
public string Uid { get; set; }
}
public class Location
{
[JsonProperty("lat")]
public double Lat { get; set; }
[JsonProperty("lng")]
public double Lng { get; set; }
}
#endregion
}
執行結果如圖: