Getting Latitude, Longitude and Elevation based on an Address
smarteez17 opened this issue · comments
I very new to this and I need some help. is there a method that if i pass and address i get back Latitude, Longitude and Elevation?
if yes can you show a small example
I looked at ElevationRequest Class and it need points is there a method I can call to get this based on a address ?
I also looked at GeocodeRequest passing the address i only get back Coordinates under points no Elevation
There isn't a single service that provides both the coordinate and elevation for an address. This has to be done making two services calls. First to the geocoding service, then to the elevation service. Here is a code snippet, assuming your address is "1 Microsoft way, Redmond, WA"
//Add your Bing Maps key.
var BING_MAPS_KEY = "YOUR_BING_MAPS_KEY";
//Create a request.
var request = new GeocodeRequest()
{
Query = "1 Microsoft way, Redmond, WA",
MaxResults = 1, //Only requesting one result in this case as I just want the first result. No need for more, keep the response size small.
BingMapsKey = BING_MAPS_KEY
};
//Process the request by using the ServiceManager.
var response = await request.Execute();
if(response != null &&
response.ResourceSets != null &&
response.ResourceSets.Length > 0 &&
response.ResourceSets[0].Resources != null &&
response.ResourceSets[0].Resources.Length > 0)
{
var result = response.ResourceSets[0].Resources[0] as BingMapsRESTToolkit.Location;
//Extract the coordinate for the result.
var coordinate = result.Point.GetCoordinate();
//Create an elevation request for the geocoded coordinates.
var elevRequest = new ElevationRequest()
{
Points = new List<Coordinate>() {
coordinate
},
BingMapsKey = BING_MAPS_KEY
};
//Process the request by using the ServiceManager.
var elevResponse = await elevRequest.Execute();
if(elevResponse != null &&
elevResponse.ResourceSets != null &&
elevResponse.ResourceSets.Length > 0 &&
elevResponse.ResourceSets[0].Resources != null &&
elevResponse.ResourceSets[0].Resources.Length > 0)
{
var elevResult = elevResponse.ResourceSets[0].Resources[0] as BingMapsRESTToolkit.ElevationData;
//Extract the elvation information.
var elevation = elevResult.Elevations[0];
//Do something with the coordinate and elevation information.
} else {
//Unable to find an elevation for a location. This often happens if the coordinate is over water.
}
} else {
// Unable to geocode location.
}
For future assistance, I recommend asking technical questions like this in the forums as those are monitored by a lot more people:
https://docs.microsoft.com/en-us/answers/topics/azure-maps.html
thanks you I have posted the question maybe you know the answer to this
I see that there a Autosuggest that returns an Address object, I also see you have Find location by address api that also returns an Address object. My question is why both address object are not the same?
They are the same. These services have different top level response classes, but they both leverage the same Address class.
look at the api the reponse for Autosugesst is the following
and for loaction is the following
and looking that Address class from the toolkit
it look like it using the address for loaction for both Autosugesst and loaction however there are missing fielids like houseNumber and streetName( which Autosugesst has )
can this be updated ?
That documentation for the response field looks to be inaccurate, or just not nicely formatted. All of those responses have an address
property that use the same Address
object as the location API. You can see this more clearly when looking at the response samples in the docs. I don't recall seeing the houseNumber
or streetName
values returned from the autosuggest API when it was first released, this must have been added in recent times and this toolkit hasn't been updated. As for why this service returns it, but not the location APIs, I suspect that this is something new that hasn't been rolled out to all services. Note, when trying the autosuggest API I am only finding that a small subset of addresses in select countries actually return these fields. Generally, all street addresses will contain that AddressLine
property.