可以在Windows应用商店应用中使用Microsoft.Phone.Maps.Services吗?

时间:2012-12-27 03:08:52

标签: c# geolocation windows-phone-8 windows-store-apps

可以在Windows应用商店应用中使用Microsoft.Phone.Maps.Services命名空间吗?

如果没有,是否有合适的替代方案?

我被定向到this,其中显示了一些代码,这些代码正是非医生根据搜索字词(如地址)获取地理定位数据的命令。

无论其

该代码段中使用的类(在Maps_GeoCoding事件和QueryCompleted回调中)来自Microsoft.Phone.Maps.Services命名空间,我需要这个或类似的代码用于Windows商店应用程序应用程序(我知道“Windows应用商店应用程序”的命名会导致一些尴尬)。

有人知道一组类似的功能吗?或者,有可能,虽然反直觉的声音,实际上可以在Windows应用商店应用程序应用程序中使用Microsoft.Phone.Maps.Services命名空间吗?

更新

这就是我所做的(广告[a,o]来自Justin“Teen”Angel的代码,appId和appCode未显示):

private async static Task<string> GetCoordinatesForAddress(string address) // AKA Geocoding (reverse geocoding is getting address for coordinates)
{
    // build URL for Here.net REST service
    string currentgeoLoc = "0.0,0.0";
    string queryString = address; //"Ferry Building, San-Francisco";
    string appID = "<appId>"; // MAKE SURE TO GET YOUR OWN from developers.here.net
    object appCode = "<appCode>"; // MAKE SURE TO GET YOUR OWN from developers.here.net
    var hereNetUrl = string.Format(
        "http://demo.places.nlp.nokia.com/places/v1/discover/search?at={0}&q={1}&app_id={2}&app_code={3}&accept=application/json",
            currentgeoLoc, queryString, appID, appCode);

    // get data from HERE.net REST API
    var httpClient = new HttpClient();
    var hereNetResponse = await httpClient.GetStringAsync(hereNetUrl);

    // deseralize JSON from Here.net 
    using (var tr = new StringReader(hereNetResponse))
    using (var jr = new JsonTextReader(tr))
    {
        var rootObjectResponse = new JsonSerializer().Deserialize<JsonDOTNetHelperClasses.RootObject>(jr);
        var firstplace = rootObjectResponse.results.items.First();
        return string.Format("{0};{1}", firstplace.position[0], firstplace.position[1]);
    }
}

2 个答案:

答案 0 :(得分:2)

WP8诺基亚<Maps />控件及其相关服务(路由,地理编码等)目前在Win8 SDK中不可用。 Win8应用程序应使用Bing Maps API。

但是,如果您确实想在Win8应用中使用诺基亚地图功能,那绝对可以。 Here.net(诺基亚的位置门户网站)公开了公开记录的Web API。您可以使用"core plan",每天允许来自here.net REST API的最多2,500次免费查询。这些REST API包括地理编码,反向地理编码,行人路线,驾驶路线等。

您可以看到这些REST API @ http://developer.here.net/javascript_api_explorer的示例(单击右上角的“REST API Explorer”,因为此视图默认为javascript API资源管理器)。地理编码API将在“地点”下提供。

例如,以下是如何在Win8上使用REST API复制WP8 Maps GeoCoding sample

private async void GeocodingWin8Query()
{
    // build URL for Here.net REST service
    string currentgeoLoc = "0.0,0.0";
    string queryString = "Ferry Building, San-Francisco";
    string appID = "<appId>"; // MAKE SURE TO GET YOUR OWN from developers.here.net
    object appCode = "<appCode>"; // MAKE SURE TO GET YOUR OWN from developers.here.net
    var hereNetUrl = string.Format(
        "http://demo.places.nlp.nokia.com/places/v1/discover/search?at={0}&q={1}&app_id={2}&app_code={3}&accept=application/json",
            currentgeoLoc, queryString, appID, appCode);

    // get data from HERE.net REST API
    var httpClient = new HttpClient();
    var hereNetResponse = await httpClient.GetStringAsync(hereNetUrl);

    // deseralize JSON from Here.net 
    using (var tr = new StringReader(hereNetResponse))
    using (var jr = new JsonTextReader(tr))
    {
        var rootObjectResponse = new JsonSerializer().Deserialize<RootObject>(jr);

        // print the details of the first geocoding result 
        var firstplace = rootObjectResponse.results.items.First();
        await new MessageDialog("Name: " + firstplace.title + Environment.NewLine +
                          "Geolocation: " + firstplace.position[0] + ", " + firstplace.position[1] + Environment.NewLine +
                          "Address: " + HtmlUtilities.ConvertToText(firstplace.vicinity) + Environment.NewLine +
                          "Type: " + firstplace.type + Environment.NewLine,
                          "Win8 Nokia Maps Geocoding").ShowAsync();
    }
}

当我们运行此代码段时,我们可以看到Win8可以访问与WP8相同的地理编码数据:

Win8 GeoCoding data from REST APIs

此API可以执行更多操作,例如反向地理编码,路由等。正如我所提到的,您可以在Here.net REST APIs here(点击右上角的“REST API资源管理器”)中探索这些功能。此外,请勿在登录后忘记注册AppID和AppCode。

对于上面的代码,我使用了JSON.Net。您需要install JSON.net from NuGet并从json2csharp复制一些强类型生成的类。以下是如何安装JSON.net:

NuGet installing Json.net

以下是生成的C#JSON.net类:

public class Category
{
    public string id { get; set; }
    public string title { get; set; }
    public string href { get; set; }
    public string type { get; set; }
}

public class Item
{
    public List<double> position { get; set; }
    public int distance { get; set; }
    public string title { get; set; }
    public Category category { get; set; }
    public string icon { get; set; }
    public string vicinity { get; set; }
    public List<object> having { get; set; }
    public string type { get; set; }
    public string href { get; set; }
    public string id { get; set; }
    public double? averageRating { get; set; }
}

public class Results
{
    public List<Item> items { get; set; }
}

public class Location
{
    public List<double> position { get; set; }
}

public class Context
{
    public Location location { get; set; }
    public string type { get; set; }
}

public class Search
{
    public Context context { get; set; }
}

public class RootObject
{
    public Results results { get; set; }
    public Search search { get; set; }
}

答案 1 :(得分:1)

虽然有可能,但我认为它不可靠。

如果内置了Maps SDK(在Bing Maps上运行),您为什么要这样做?

Here's一个教程 我想你应该看看