📄 search.cs
字号:
// Copyright (c) Jason Fuller.
//
// Use of this source code is subject to the terms of the license found in the
// file License.txt. If you did not accept the terms of that license, you are
// not authorized to use this source code.
#region Using directives
using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
#endregion
namespace PocketEarth
{
/// <summary>
/// A static class used to make search requests against the
/// VirtualEarth server.
/// </summary>
public class Search
{
private Search()
{
}
/// <summary>
/// Search for an address or the name of a address, and return the bounding
/// rectangle of the result.
/// </summary>
/// <param name="address">The address or address name.</param>
/// <param name="lat1">The first latitude of the bounding rectangle of the search result.</param>
/// <param name="long1">The first longitude of the bounding rectangle of the search result.</param>
/// <param name="lat2">The second latitude of the bounding rectangle of the search result.</param>
/// <param name="long2">The second longitude of the bounding rectangle of the search result.</param>
/// <returns></returns>
public static bool SearchForAddress(
string address,
out double lat1,
out double long1,
out double lat2,
out double long2)
{
lat1 = lat2 = long1 = long2 = 0;
string searchParams =
"a=&b=" + address + "&c=0.0&d=0.0&e=0.0&f=0.0&g=&i=&r=0";
string results = DoSearchRequest(searchParams);
if (results == null || results == string.Empty)
{
return false;
}
// Parse results
Regex r = new Regex(@"SetViewport\((?<lat1>\S+),(?<long1>\S+),(?<lat2>\S+),(?<long2>\S+)\)");
Match m = r.Match(results);
if (!m.Success)
{
return false;
}
lat1 = double.Parse(m.Groups["lat1"].Value);
long1 = double.Parse(m.Groups["long1"].Value);
lat2 = double.Parse(m.Groups["lat2"].Value);
long2 = double.Parse(m.Groups["long2"].Value);
return true;
}
/// <summary>
/// Search for a business or business category.
/// </summary>
/// <param name="business">The business.</param>
/// <param name="lat1">The first latitude of the bounding rectangle to search within.</param>
/// <param name="lon1">The first longitude of the bounding rectangle to search within.</param>
/// <param name="lat2">The second latitude of the bounding rectangle to search within.</param>
/// <param name="lon2">The second longitude of the bounding rectangle to search within.</param>
/// <returns>An ArrayList of BusinessSearchResults.</returns>
public static ArrayList SearchForBusiness(
string business,
double lat1,
double lon1,
double lat2,
double lon2)
{
// Are there any serach results after this batch?
bool moreToCome;
// The index into the full set of search results.
int startIndex = 0;
// The maximum number of serach results to return.
// This number was chosen fairly arbitrarily.
const int maxResultsCount = 50;
// The HTTP result string containing the search results.
string results;
// The search results to return.
ArrayList searchResults = new ArrayList();
do
{
moreToCome = false;
string searchParams =
"a=" + business.Trim() +
"&b=" + "" + // where
"&c=" + lat1 + // bounding rectangle to search
"&d=" + lon1 +
"&e=" + lat2 +
"&f=" + lon2 +
"&g=" + startIndex + // startIndex == which of the full set of search
// results do you want the server to return
// first in the list?
"&i=" + "0" + // index == ?
"&r=" + "false"; // ?
results = DoSearchRequest(searchParams);
if (results != null && results != string.Empty)
{
// Parse the results.
Regex r = new Regex(@"VE_SearchResult\((?<id>[0-9]*),'(?<name>[^']*)','(?<address>[^']*)','(?<phone>[^']*)',(?<rating>[^,]*),'(?<type>[^']*)',(?<latitude>[^,]*),(?<longitude>[^,)]*)\)");
MatchCollection matches = r.Matches(results);
foreach (Match m in matches)
{
searchResults.Add(new BusinessSearchResult(
m.Groups["name"].Value,
m.Groups["address"].Value,
m.Groups["phone"].Value,
double.Parse(m.Groups["latitude"].Value),
double.Parse(m.Groups["longitude"].Value)));
}
// The server always returns 10 at a time.
startIndex += 10;
// The "true" means there are more search results
// available on the server.
r = new Regex(@"true,''\);$");
if (r.IsMatch(results))
{
moreToCome = true;
}
}
} while (moreToCome && searchResults.Count < maxResultsCount);
return searchResults;
}
/// <summary>
/// Make a search request to the VirtualEarth server.
/// </summary>
/// <param name="searchParams">The string of parameters.</param>
/// <returns>The search results.</returns>
private static string DoSearchRequest(string searchParams)
{
string results = String.Empty;
HttpWebRequest searchRequest =
(HttpWebRequest)WebRequest.Create("http://207.46.159.133/search.ashx");
searchRequest.Method = "POST";
//searchRequest.ServicePoint.Expect100Continue = false;
searchRequest.ContentType = "application/x-www-form-urlencoded";
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
byte[] bytes = encoding.GetBytes(searchParams);
searchRequest.ContentLength = bytes.Length;
try
{
Stream requestStream = searchRequest.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
results = GetSearchResults(searchRequest);
}
catch (WebException)
{
}
return results;
}
/// <summary>
/// Retrieve the results of the specified web request.
/// </summary>
/// <param name="searchRequest">The web request.</param>
/// <returns>The HTTP result string.</returns>
private static string GetSearchResults(HttpWebRequest searchRequest)
{
string results = string.Empty;
HttpWebResponse searchResponse;
searchResponse =
(HttpWebResponse)searchRequest.GetResponse();
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
Stream receiveStream = searchResponse.GetResponseStream();
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(receiveStream, encode);
Char[] read = new Char[256];
// Reads 256 characters at a time.
int count = readStream.Read(read, 0, 256);
while (count > 0)
{
String str = new String(read, 0, count);
results += str;
count = readStream.Read(read, 0, 256);
}
searchResponse.Close();
return results;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -