如何在Windows Phone 8.1中使用查询API获取附近位置

时间:2015-10-19 10:46:58

标签: c# visual-studio-2013 windows-phone-8.1 bing-maps

使用此query api如何在Windows Phone 8.1中将附近的地点放入我们的地图控件中。当我从上面的链接放置代码时,它显示404错误(即找不到资源)。

我得到了地图键。但是如何放置访问ID。如何命名数据源。请帮帮我。

string BingMapsKey = "MY_BING_MAPS_KEY";

string DataSourceID = "20181f26d9e94c81acdf9496133d4f23";


string dataSourceName = "petrolbunk";
string dataEntityName = "petrolbunk";
string accessId = DataSourceID;
string bingMapsKey = BingMapsKey;
double SearchLatitude = 47.63674;
double SearchLongitude = -122.30413;
double Radius = 3;

string requestUrl1 = string.Format("http://spatial.virtualearth.net/REST/v1/data/{0}/{1}/{2}" + "?spatialFilter=nearby({3},{4},{5})&key={6}", accessId, dataSourceName,
dataEntityName, SearchLatitude, SearchLongitude, Radius, bingMapsKey);

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

HttpResponseMessage response = await client.GetAsync(requestUrl1);
if (response.IsSuccessStatusCode)
{
    var data = response.Content.ReadAsStringAsync();

以上网址布局是否正确。如何在我们的地图控件中显示它们。

2 个答案:

答案 0 :(得分:1)

以下代码清单使用NAVTEQEUDataSource在附近进行搜索。你可以把它作为例子

internal class NAVTEQEUDataSource : INAVTEQEUDataSource
{
    public async Task<IList<Geopoint>> SearchNearBy(double latitude, double longitude, double radius, int entityTypeId, int maxResult, string bingMapKey)
    {
        const string spatialBaseUrl = "http://spatial.virtualearth.net/REST/v1/data/";
        string url =
            "c2ae584bbccc4916a0acf75d1e6947b4/NavteqEU/NavteqPOIs?spatialFilter=nearby({0},{1},{2})&$filter=EntityTypeID%20eq%20'{3}'&$select=EntityID,DisplayName,Latitude,Longitude,__Distance&$top={4}&key={5}";
        HttpClient httpClient = new HttpClient { BaseAddress = new Uri(spatialBaseUrl) };
        url = string.Format(url, latitude, longitude, radius, entityTypeId, maxResult, bingMapKey);
        string response = await httpClient.GetStringAsync(url);
        XmlUtil xmlUtil = new XmlUtil(response);
        IList<XElement> properties = xmlUtil.GetElements("entry").ToList();
        IList<Geopoint> result = new List<Geopoint>();
        foreach (var property in properties)
        {
            BasicGeoposition basicGeoposition = new BasicGeoposition();

            double temp;
            if (double.TryParse(xmlUtil.GetRelativeElement(property, "content.properties.Latitude").Value, out temp))
                basicGeoposition.Latitude = temp;
            if (double.TryParse(xmlUtil.GetRelativeElement(property, "content.properties.Longitude").Value, out temp))
                basicGeoposition.Longitude = temp;
            result.Add(new Geopoint(basicGeoposition));
        }

        return result;
    }
}

如何运作您可以在此post阅读更多内容。代码清单使用其他一些util类来处理响应,所以我认为你应该直接阅读该帖子。

答案 1 :(得分:1)

查看您的帐户,看起来没有任何可以访问的数据源

http://spatial.virtualearth.net/REST/v1/data/20181f26d9e94c81acdf9496133d4f23/petrolbunk/petrolbunk

您是否创建了数据源?如果您有,请在Bing Maps门户中转到数据源 - &gt;查看数据源信息。找到您要访问的数据源并复制完整的URL(您不需要将其分解为多个部分,即accessId,数据源名称,实体名称)。

如果您想访问Bing Maps中的某个兴趣点数据源,请查看NAVTEQNA数据源:https://msdn.microsoft.com/en-us/library/hh478192.aspx