2011-11-24 81 views
2

我使用Bingmaps API獲取緯度和經度的AdminDistrict和CountryRegion。 它可以在瀏覽器中輸入這個網址:在Bingmaps上輸入來自經緯度的位置數據時出錯

http://dev.virtualearth.net/REST/v1/Locations/-30,-70/?includeEntityTypes=AdminDivision1,CountryRegion&o=xml&c=es-ES&key=myBingmapsApiKey

但在C#中的WP7我不能得到它的工作。 這是代碼:

string wsUrl = "http://dev.virtualearth.net/REST/v1/Locations/-30,-70/?includeEntityTypes=AdminDivision1,CountryRegion&o=xml&c=es-ES&key=*myBingmapsApiKey*"; 

var request = new RestSharp.RestRequest(Method.GET); 
var client = new RestSharp.RestClient(wsUrl); 

try 
{ 
    RestSharp.RestResponse resource; 
    client.ExecuteAsync(request, (response) => 
    { 
     resource = response; 
     string content = resource.Content; 
     string status_code = resource.StatusCode.ToString(); 
     string response_status = resource.ResponseStatus.ToString(); 
    }); 
} 
catch (Exception e) 
{ 
    string error = "Error: " + e.ToString() + "\n. Stack Trace: " + e.StackTrace; 
} 

和響應是:

<?xml version="1.0" encoding="utf-8"?> 
    <Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1"> 
<Copyright>Copyright © 2011 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.</Copyright> 
<BrandLogoUri>http://dev.virtualearth.net/Branding/logo_powered_by.png</BrandLogoUri> 
<StatusCode>401</StatusCode><StatusDescription>Unauthorized</StatusDescription> 
<AuthenticationResultCode>InvalidCredentials</AuthenticationResultCode> 
<ErrorDetails><string>Access was denied. You may have entered your credentials incorrectly, or you might not have access to the requested resource or operation.</string></ErrorDetails> 
<TraceId>59ebcf604bb343d79a6e8b93ad5695fe|MIAM001452|02.00.71.1600|</TraceId> 
<ResourceSets /> 
    </Response> 

的網址是正在Web瀏覽器相同。 什麼可能是錯的?

回答

0

如果我理解這個權利,那麼您使用的REST API may cost money。也許你的API密鑰沒有設置用於計費交易?

的網頁說,有關地點API的計費方式:

*如果AJAX控件或Silverlight控制會話的上下文中出現呢?這類別不是計費。

也許瀏覽器算作AJAX控件,手機並不完全是「Silverlight控件」。

3

可能在這一點上,你已經想出了一個解決方案,但在谷歌上搜索我發現這個話題,解決方案是不像你那樣通過URL發送密鑰,而是將其作爲參數添加到請求中,如下所示:

string wsUrl = "http://dev.virtualearth.net/REST/v1/Locations/-30,-70/";  

var request = new RestSharp.RestRequest(Method.GET);  
request.AddParameter("includeEntityTypes", "AdminDivision1,CountryRegion"); 
request.AddParameter("key", myLey); 
request.AddParameter("o", "xml");