Result.Location プロパティ (Search)
PhoneBook または QueryLocation の応答に関連付けられた Location オブジェクトを返します。
構文
public Location Location { get; set; }
解説
ホワイト ページ (個人別) またはイエロー ページ (職業別) のエントリと共に返される Location オブジェクトには、そのエントリの住所の経度と緯度が格納されています。一緒に返される Location.Radius フィールドには、意味のある結果は含まれず、返される半径は常に既定値の 5 に設定されます。
Location オブジェクトとそれに含まれるフィールドの詳細については、「Location クラス (Search)」トピックを参照してください。
例
この例では、PhoneBook と QueryLocation SourceType から Location 情報を返して、処理する方法を示しています。
try
{
MSNSearchService s = new MSNSearchService();
SearchRequest searchRequest = new SearchRequest();
int arraySize = 2;
SourceRequest[] sr = new SourceRequest[arraySize];
sr[0] = new SourceRequest();
sr[0].Source = SourceType.QueryLocation;
sr[0].ResultFields = ResultFieldMask.All;
sr[1] = new SourceRequest();
sr[1].Source = SourceType.PhoneBook;
sr[1].ResultFields = ResultFieldMask.All;
sr[1].Count = 3;
// Developer Provisioning System で指定されたアプリケーション ID を 二重引用符で囲って
// SearchRequest の AppID の値として入力します。
searchRequest.AppID = "YOUR_APP_ID_GOES_HERE";
searchRequest.Flags = SearchFlags.MarkQueryWords;
searchRequest.CultureInfo = "en-US";
searchRequest.Query = "chinese food toms river nj";
searchRequest.Requests = sr;
SearchResponse searchResponse;
searchResponse = s.Search(searchRequest);
foreach (SourceResponse sourceResponse in searchResponse.Responses)
{
Result[] sourceResults = sourceResponse.Results;
if (searchResponse.Responses[0].Total > 0)
{
Console.WriteLine(sourceResponse.Source.ToString() + " - 結果総数:" + sourceResponse.Total.ToString());
//Console.WriteLine("国/地域:" + searchRequest.CultureInfo);
Console.WriteLine();
}
foreach (Result sourceResult in sourceResults)
{
if ((sourceResult.Title != null) && (sourceResult.Title != String.Empty))
Console.WriteLine("タイトル:" + sourceResult.Title);
if ((sourceResult.Description != null) && (sourceResult.Description != String.Empty))
Console.WriteLine("説明:" + sourceResult.Description);
if ((sourceResult.Url != null) && (sourceResult.Url != String.Empty))
Console.WriteLine("Url:" + sourceResult.Url);
if (sourceResult.Address != null)
{
if ((sourceResult.Address.AddressLine != null) && (sourceResult.Address.AddressLine != String.Empty))
Console.WriteLine("番地:" + sourceResult.Address.AddressLine);
if ((sourceResult.Address.CountryRegion != null) && (sourceResult.Address.CountryRegion != String.Empty))
Console.WriteLine("国/地域:" + sourceResult.Address.CountryRegion);
if ((sourceResult.Address.PostalCode != null) && (sourceResult.Address.PostalCode != String.Empty))
Console.WriteLine("郵便番号:" + sourceResult.Address.PostalCode);
if ((sourceResult.Address.PrimaryCity != null) && (sourceResult.Address.PrimaryCity != String.Empty))
Console.WriteLine("都市:" + sourceResult.Address.PrimaryCity);
if ((sourceResult.Address.SecondaryCity != null) && (sourceResult.Address.SecondaryCity != String.Empty))
Console.WriteLine("第二都市:" + sourceResult.Address.SecondaryCity);
if ((sourceResult.Address.Subdivision != null) && (sourceResult.Address.Subdivision != String.Empty))
Console.WriteLine("州:" + sourceResult.Address.Subdivision);
}
if (sourceResult.Location != null)
{
Console.WriteLine("緯度:" + sourceResult.Location.Latitude.ToString());
Console.WriteLine("経度:" + sourceResult.Location.Longitude.ToString());
// "大圏" 距離を計算します。これは、QueryLocation.Location オブジェクトに返される Location と、
// PhoneBook クエリで返される各 Location の経度と緯度の間の距離
// (マイルとキロメーター単位) です。
double earthRadius = 3963.0;
double earthRadiusKM = 6392.0;
double latitudeIn = sourceResults[0].Location.Latitude;
double longitudeIn = sourceResults[0].Location.Longitude;
double latitudeOut = sourceResult.Location.Latitude;
double longitudeOut = sourceResult.Location.Longitude;
double radianConversion = (Math.PI / 180.0);
double operandOne;
double operandTwo;
double acosOfSum;
double finalValue;
double finalValueKM;
operandOne = Math.Cos(radianConversion * (90.0 - latitudeIn)) * Math.Cos(radianConversion * (90.0 - latitudeOut));
operandTwo = Math.Sin(radianConversion * (90.0 - latitudeIn)) * Math.Sin(radianConversion * (90.0 - latitudeOut)) * Math.Cos(radianConversion * (longitudeIn - longitudeOut));
acosOfSum = Math.Acos(operandOne + operandTwo);
finalValue = earthRadius * acosOfSum;
finalValueKM = earthRadiusKM * acosOfSum;
if (sourceResponse.Source == SourceType.PhoneBook)
{
Console.WriteLine("距離 (マイル):" + finalValue.ToString());
Console.WriteLine("距離 (キロメートル):" + finalValueKM.ToString());
}
}
Console.WriteLine("*****************************************************");
}
if (sourceResponse.Source == SourceType.PhoneBook)
{
Console.WriteLine("終了するにはいずれかのキーを押してください。");
Console.ReadLine();
}
}
}
catch (SoapException fault)
{
Console.WriteLine(fault.Detail.InnerText.ToString() + ".");
Console.WriteLine("終了するにはいずれかのキーを押してください。");
Console.ReadLine();
}
catch (WebException webx)
{
Console.WriteLine(webx.ToString());
}
以下の出力の例は、このクエリの結果を示します。
QueryLocation - 結果総数: 1
タイトル:chinese food
説明:Toms River, NJ
緯度: 39.952849
経度: -74.1937
*****************************************************
PhoneBook - 結果総数: 4417
タイトル:Best ?Food?In Town ?Chinese?
説明:(732) 818-7833 - 399 Dover Rd, Toms River, NJ
Url:https://search.msn.com:80/local/details.aspx?q=Best+Food+In+Town+Chinese&near=near&lat=39.952849&lon=-74.193700&id=YPhttp%3a%2f%2fpbsearchmsn.com%2f1%2f7328187833%2fBest%2fFood%2fIn%2fTown%2fChinese%2fToms%2fRiver%2fNJ%2f08757%2f399%2fDover%2fRd&dn=Best+Food+In+Town+Chinese
番地:399 Dover Rd
国/地域:US
都市:Toms River
州:NJ
緯度: 39.9406
経度: -74.214099
距離 (マイル):5.90533018112183E-05
距離 (キロメートル):9.52482223510742E-05
*****************************************************
タイトル:Great Wall ?Chinese??Food?
説明:(732) 244-1928 - 212 Atlantic City Blvd, Pine Beach, NJ
Url:https://search.msn.com:80/local/details.aspx?q=Great+Wall+Chinese+Food&near=near&lat=39.952849&lon=-74.193700&id=YPhttp%3a%2f%2fpbsearchmsn.com%2f1%2f7322441928%2fGreat%2fWall%2fChinese%2fFood%2fPine%2fBeach%2fNJ%2f08741%2f212%2fAtlantic%2fCity%2fBlvd&dn=Great+Wall+Chinese+Food
番地:212 Atlantic City Blvd
国/地域:US
都市:Pine Beach
州:NJ
緯度: 39.932938
経度: -74.180272
距離 (マイル): 1.87063278617349
距離 (キロメートル): 3.01718010830707
*****************************************************
タイトル:New Hey Bird ?Chinese?Restrnt
説明:(732) 506-6866 - 1214 Route 37 E, Toms River, NJ
Url:https://search.msn.com:80/local/details.aspx?q=New+Hey+Bird+Chinese+Restrnt&near=near&lat=39.952849&lon=-74.193700&id=YPhttp%3a%2f%2fpbsearchmsn.com%2f1%2f7325066866%2fNew%2fHey%2fBird%2fChinese%2fRestrnt%2fToms%2fRiver%2fNJ%2f08753%2f1214%2fRoute%2f37%2fE&dn=New+Hey+Bird+Chinese+Restrnt
番地:1214 Route 37 E
国/地域:US
都市:Toms River
州:NJ
緯度: 39.953871
経度: -74.160842
距離 (マイル): 2.9694532776893
距離 (キロメートル): 4.78948911203382
*****************************************************
終了するにはいずれかのキーを押してください。