For my studying and university I have a project where I need to make an application using C# WPF. This application must allow me to select a path between two adresses and show the path on the map like google map.
How can I implement the google webservice API ? I am a bit lost with it and I don't understand how I can make my application and fit in the google map itself.
With this i must be able to calculate the distance as well.
This is what i have done with the webBrowser but it displays the whole website of google maps:
I know that the WebBrowser control can display the google maps but it doesnt actually work
This is my code actually
private void btnCharger_Click(object sender, RoutedEventArgs e)
{
//Use static or WebControl we don't know yet.
//This is the case we use webControl
string street = "";
string city = "";
string zipcode = "";
StringBuilder adresseQuery = new StringBuilder();
adresseQuery.Append("http://maps.google.com/maps?q=");
street = tbStreet.Text.Replace(" ", "+");
adresseQuery.Append(street + ",+");
city = tbCity.Text;
adresseQuery.Append(city + ",+");
zipcode = tbZipCode.Text;
webBrowser1.Navigate(adresseQuery.ToString());*/
}
So in this code i have created the adresse and sent it to the webbrowser through google maps. But is shows the whole google map page with the left bar and everything. I would like to only display the map ! How can i only display the map and not the bars present on the https://maps.google.com/
I have already checked this Link and am currently working on it but this is static.
For the part of routing/getting the directions (and for some of others functions you need as well), you can use a .NET wrapper around Google Maps API:
GoogleApi
google-maps
gmaps-api-net is outdated (at this time of answering) - the last update for the Directions API was made in 2016.
Usage example for GoogleApi:
using GoogleApi.Entities.Common;
using GoogleApi.Entities.Maps.Directions.Request;
using GoogleApi.Entities.Maps.Directions.Response;
public void GetRoute()
{
DirectionsRequest request = new DirectionsRequest();
request.Key = "AIzaSyAJgBs8LYok3rt15rZUg4aUxYIAYyFzNcw";
request.Origin = new Location("Brasov");
request.Destination = new Location("Merghindeal");
var response = GoogleApi.GoogleMaps.Directions.Query(request);
Console.WriteLine(response.Routes.First().Legs.First().DurationInTraffic);
Console.WriteLine(response.Routes.First().Legs.First().Distance);
Console.WriteLine(response.Routes.First().Legs.First().Steps);
}
Related
I am developing a UWP app using the Maps control that allows a user to plan a route by adding waypoints on the map using various methods such as clicking on the UWP Map control. One of the ways I want to allow a user to add a waypoint or location is to search by actual address. I use the BING Maps REST services code below but if I don't supply the language and culture of where the app is currently being used it always returns American addresses first which are clearly no use to users not in the USA (Yes Microsoft, some of us actually live outside the USA - shock, horror!). I discovered if I supplied the language-culture string such as "en-AU" for Australia then it will search Australian addresses first, and this works really well.
public async Task<List<WayPoint>> FindAddress(string address)
{
List<WayPoint> matchingWaypoints = new List<WayPoint>();
//Create a Geocode request to submit to the BING Maps REST service.
var request = new GeocodeRequest()
{
Query = address.Trim(),
Culture = "en-AU", //HOW CAN I GET THIS FROM THE DEVICE'S CURRENT LOCATION???
IncludeIso2 = true,
IncludeNeighborhood = true,
MaxResults = 10,
BingMapsKey = MapServiceToken
};
//Process the request by using the BING Maps REST services.
var response = await ServiceManager.GetResponseAsync(request);
if (response != null &&
response.ResourceSets != null &&
response.ResourceSets.Length > 0 &&
response.ResourceSets[0].Resources != null &&
response.ResourceSets[0].Resources.Length > 0)
{
int wpNumber = 0;
foreach (BingMapsRESTToolkit.Location loc in response.ResourceSets[0].Resources)
matchingWaypoints.Add(new WayPoint(wpNumber++, loc.Address.FormattedAddress, loc.Point.Coordinates[0], loc.Point.Coordinates[1]));
}
return matchingWaypoints;
}
So what I obviously want to do is derive this string based on the device's current location (i.e: country) NOT from the device's region settings. So for example if someone is using the app the USA I want to specify en-US, if they're in New Zealand it would be en-NZ, if in France it would be "fr-FR" etc. Does anyone know how I could do this? All the stuff I've read about localisation uses the device settings NOT the current physical location so I'm still trying to work out how to do it.
If anyone can help out I'd really appreciate it :-)
There is an official sample of how to get location at https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/Geolocation
This will return the latitude and longitude. You can then use a different Bing API to get the country based on that lat/long.
This will give you a country name. With this you can lookup against a pre-generated list of codes to country names (created with CultureInfo.GetCultures() on your machine) or you could look at this for a way of doing this at runtime as GetCultures() isn't supported in UWP.
Using the Regional settings is simpler but if you want to use the actual location then this is the way to go. For devices where access to location isn't available then regional settings could be a good back up.
Another approach would be one of various public APIs which will give you information such as location based on IP address. This also isn't perfect though due to the use of proxies in different countries, etc.
Thanks for the reply Matt but I worked out how to do it with the help of the Bing Maps forum. Instead of using the REST API am using the FindLocationsAsync call as part of the UWP Map API passing in the address, the local geopoint to use a starting reference (aka a 'hint' location) and the max number of matches to return... here's the code I used which works perfectly. (Note that WayPoint is an object from my model to store various info about a waypoint on the route.)
public async Task<List<WayPoint>> FindAddress(string address)
{
List<WayPoint> matchingWaypoints = new List<WayPoint>();
// Use current location as a query hint so nearest addresses are returned.
BasicGeoposition queryHint = new BasicGeoposition();
queryHint.Latitude = this.CurrentLocation.Position.Latitude;
queryHint.Longitude = this.CurrentLocation.Position.Longitude;
Geopoint hintPoint = new Geopoint(queryHint);
MapLocationFinderResult result =
await MapLocationFinder.FindLocationsAsync(address.Trim(), hintPoint, 5);
// If the query returns results, store in collection of WayPoint objects.
string addresses = "";
if (result.Status == MapLocationFinderStatus.Success)
{
int i = 0;
foreach (MapLocation res in result.Locations)
{
matchingWaypoints.Add(new WayPoint(i++, res.Address.FormattedAddress, res.Point.Position.Latitude, res.Point.Position.Longitude));
}
}
return matchingWaypoints;
}
Good day everyone. I am pretty new to Mobile Development using C# and XAMARIN. My problem is I am trying to get the current user's first name from Parse.com database and display it on a textview, but I am getting nothing. Here's my code for getting the user data:
public async void getData(){
var ProfileName = await ParseObject.GetQuery ("User")
.WhereEqualTo ("FirstName", ParseUser.CurrentUser).FindAsync ();
string ProfName = ProfileName.ToString ();
name.Text = ProfName; //name.Text is the text view
}
Thank you everyone.
it's me again. After diving deep down into stackoverflow, I have stumbled upon a question. It is somewhat similar to what I'm looking for and it solved my problem. So here's my modified code that works, it display the First name of the currently logged in user.
public void getData(){
string parseUser = ParseUser.CurrentUser.Get<string> ("FirstName");
name.Text = parseUser;
}
I currently have the following method being called whenever a location is selected.
Instead of passing the city name as the parameter to search by, I would like to search locations by using latitudes and longitudes.
I will be retrieving the latitude and longitude of the selected city.
How would I need to modify this code in order to obtain this?
Thank you
private void xgrdLocation_SelectedItemChanged(object sender, SelectedItemChangedEventArgs e)
{
this.Cursor = Cursors.AppStarting;
lblFooter.Content = "Searching ...";
pushpin = new MapPushpin();
if (xgrdLocation.SelectedItem != null)
{
City selectedCity = xgrdLocation.SelectedItem as City;
//GeocodeService.Location point = new GeocodeService.Location();
pushpin.Text = selectedCity.CityName;
searchDataProvider.Search(pushpin.Text);
//lblSelectedCity.Content = selectedCity.CityName;
}
}
If you want to get the city that a latitude/longitude value is in, you can use the Reverse Geocoding. The Bing Maps REST Location API has such a feature. https://msdn.microsoft.com/en-us/library/ff701710.aspx
You can also find documentation on how to use the REST services in .NET here: https://msdn.microsoft.com/en-us/library/jj819168.aspx
Not sure, but it looks like you might be using the old legacy SOAP services in your application to do the geocoding currently. Avoid the SOAP services, they are old, slow and nearing end of life.
I'm doing a school project, I need to make a simple web site, add google maps on it, read, lets say, 100 diffrent addresses from a text file and show those locations on google maps with markers.
Now I'm trying to add google maps to my ASP.net page with javascript which I saw on google maps tutorials. And there's that problem which I have to convert adresses to coordinates. So for that I'm using
function addAddressToMap(response) {
if (!response || response.Status.code != 200) {
alert("Sorry, we were unable to geocode that address");
}
else {
place = response.Placemark[0];
point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
marker = new GMarker(point);
map.addOverlay(marker);
marker.openInfoWindowHtml(place.address + '<br>' + '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
}
}
// showLocation() is called when you click on the Search button
// in the form. It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation() {
var address = "izmit";
var address2 = "ağrı";
geocoder.getLocations(address, addAddressToMap);
geocoder.getLocations(address2, addAddressToMap);
}
these functions and they are working fine. But my problem here is, I need to get these address informations from a text file. But to get them, I need to use a few diffrent codes. And I want to make it on the server side with C# codes. But I don't know how to write some codes on server side and then return something to HTML side as address. I hope you understand. Thank you for your helps.
Update:
Server code:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterArrayDeclaration("Skills", "'asa'");
Page.ClientScript.RegisterArrayDeclaration("Skills", "'bell'");
Page.ClientScript.RegisterArrayDeclaration("Skills", "'C'");
Page.ClientScript.RegisterArrayDeclaration("Skills", "'C++'");
}
}
Client side:
function showLocation()
{
var address = "izmit";
var address2 = "ağrı";
geocoder.getLocations(Skills[0], addAddressToMap);
}
Now if I use "asa" instead of Skills[0] it will show the location and mark, but with Skills[0] it's not working. And thank you for your answer that was what I'm looking for.
even if I try var MyValue = Skills[0]; and then use MyValue instead of Skills[0] it's still not working
If I understood your question correctly, you want to create an array on the server side and read it in the client.
See this link for a tutorial on how to pass an array from the server to the client.
Basically, you want to use the ClientScriptManager.RegisterArrayDeclaration method to add values to the array.
You can then easily read it in javascript.
Server Side:
string arrayName = "MyArray";
Page.ClientScript.RegisterArrayDeclaration(arrayName , "'value1'");
Page.ClientScript.RegisterArrayDeclaration(arrayName , "'value2'");
Javascript on Client Side:
function readArray()
{
for (var i = 0; i < MyArray.length; i++)
{
//Reading Element From Array
var myValue = MyArray[i];
}
}
I'm not sure if this has even been done before, but what I am trying to accomplish can't be explained in any great detail, but basically, what I am trying to do is Process PHP scripts from within my C# Windows Forms Application.
I have already created a HTTP server, which works just fine. But now I need to be able to process PHP scripts aswell. We're working on a new privatised language over here, purely for learning and fun.
(Just a little background, not completely related):
We are now able to create a webpage like so:
Using System.Core,
System.Http,
System.Graphics and System.IO;
protected void -> Webpage(object homepage)
{
// Set Webpage properties.
homepage.Name = "Home";
homepage.Size = new Size(960[px], 100[%]);
homepage.Alignment = new AlignmentType.Vertical;
homepage.Alignment = new AlignmentType.Horizontal;
// This is a comment.
// Create objects to be rendered on the page.
Text text = new Text();
FormElements formElements = new FormElements();
private void -> Webpage.Load(object homepage)
{
text.Text = "Please enter your name below:";
text.Style = new Style(
Alignment = new AlignmentType.Horizontal,
Alignment = new AlignmentType.ManualAlignment(15[Y, px]),
Font = new Font(
Font.Family("Arial"),
Font.Size = new Size(9[pt], LineHeight(4[px])),
Font.Color = new Color.FromArgb(15, 15, 15))
);
formElements.CreateElements(TextField["textField"], SubmitButton["submitButton"], Form["form"]);
textField.Name = "name";
submitButton.Name = "submit";
form.Encapsulate(name, submit);
form.Alignment = new AlignmentType.RelativeTo(text.Bottom);
Elements[] elements = new Elements[]
{
text, form;
};
homepage.Display.Element.ElementCollection(elements);
}
private void -> Webpage.FormSubmission(object form)
{
form.Element(name).OmitSpecialCharacters();
if(form.Value is not Empty)
{
text.Text = "Hello, " + form.Element(name).Value;
}
}
}
The above sample demonstrates the ability to create a whole webpage, style it, and process form input in a nice, clean way. However, we've come to a complete dead end (trying to support PHP) and we do not wish to delve too far into server-side languages (lack of experience in that area is the main reason), so we would like to be able to "support" PHP scripts from within our WinForms app.
Anyone know of any way to process PHP scripts from within a C# winforms app?
Here is a simple http server that supports PHP:
MiniHttpd: an HTTP web server library
And here is Phalanger - The PHP Language Compiler for the .NET Framework
Use an HttpWebRequest, and request the url to the PHP page like you would to run the PHP page in your browser.
See here for more info: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx