Reputation: 10227
Can the Microsoft.Phone.Maps.Services
namespace be used in a Windows Store Apps app?
If not, is there a suitable alternative available?
I was directed to this which shows some code that's just what the non-doctor ordered for getting gobs of geolocation data based on a search term such as an address.
HOWEVER
The classes used in that snippet (in the Maps_GeoCoding
event and the QueryCompleted
callback) are from the Microsoft.Phone.Maps.Services namespace, and I need this or similar code for a Windows store apps app (I knew that "Windows Store app" nomenclature would lead to some awkwardness).
Is anybody aware of an analogous set of functionality? Or, is it possible, though counter-intuitive sounding, that one could actually use the Microsoft.Phone.Maps.Services
namespace within a Windows Store apps app?
This is what I did (ad[a,o]pted from Justin "Teen" Angel's code below, with appId and appCode not shown):
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]);
}
}
Upvotes: 0
Views: 802
Reputation: 16092
The WP8 Nokia <Maps />
control and its associated services (routing, geocoding, etc) aren't currently available in the Win8 SDK. Win8 apps are expected to use Bing Maps APIs.
However, if you do want to use Nokia Maps functionality in your Win8 app, that's definitely possible. Here.net (Nokia's location portal) exposes publicly documented web APIs. You can use the "core plan" that allows up to 2,500 free queries/day from the here.net REST APIs. Those REST APIs include geocoding, reverse geocoding, pedestrian routing, driving routing and more.
You can see examples for these REST APIs @ http://developer.here.net/javascript_api_explorer (click "REST API Explorer" in the top right since this view defaults to the javascript API explorer). The Geocoding APIs will be available under "Places".
For example, here's how to replicate the WP8 Maps GeoCoding sample using REST APIs on Win8:
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();
}
}
When we run this code snippet we can see that Win8 has access to the same Geocoding data as WP8:
There's lots more this API can do, like reverse geocoding, routing, etc. As I mentioned you can explore those features at the Here.net REST APIs here (click "REST API explorer" in the top right). Also, don't forgot to sign up for an AppID and AppCode after signing in.
For the code above to work I've used JSON.Net. You'll need to install JSON.net from NuGet and copy some strongly-typed generated classes over from json2csharp. Here's how to install JSON.net:
And here are the generated C# JSON.net classes:
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; }
}
Upvotes: 2