Cannot draw routes on MapControl (Windows Phone 8.1) - c#

I need to draw a driving route between two points on my MapControl (I'm doing this for Windows Phone 8.1). I did everything with the MSDN's tutorial but it doesn't work - the route is empty. What am I doing wrong? Here's the code.
BasicGeoposition startLocation = new BasicGeoposition();
startLocation.Latitude = 40.7517;
startLocation.Longitude = -073.9766;
Geopoint startPoint = new Geopoint(startLocation);
// End at Central Park in New York City.
BasicGeoposition endLocation = new BasicGeoposition();
endLocation.Latitude = 40.7669;
endLocation.Longitude = -073.9790;
Geopoint endPoint = new Geopoint(endLocation);
// Get the route between the points.
MapRouteFinderResult routeResult =
await MapRouteFinder.GetDrivingRouteAsync(
startPoint,
endPoint,
MapRouteOptimization.Time,
MapRouteRestrictions.None,
290
);
MapRouteView viewOfRoute = new MapRouteView(routeResult.Route);
viewOfRoute.RouteColor = Colors.Blue;
viewOfRoute.OutlineColor = Colors.Blue;
// Add the new MapRouteView to the Routes collection
// of the MapControl.
mapeczka.Routes.Add(viewOfRoute);
// Fit the MapControl to the route.
await mapeczka.TrySetViewBoundsAsync(
routeResult.Route.BoundingBox,
null,
Windows.UI.Xaml.Controls.Maps.MapAnimationKind.Bow);
if (routeResult.Status == MapRouteFinderStatus.Success)
{
// Use the route to initialize a MapRouteView.
viewOfRoute = new MapRouteView(routeResult.Route);
viewOfRoute.RouteColor = Colors.Yellow;
viewOfRoute.OutlineColor = Colors.Black;
// Add the new MapRouteView to the Routes collection
// of the MapControl.
}
mapeczka.Routes.Add(viewOfRoute);
// Fit the MapControl to the route.
await mapeczka.TrySetViewBoundsAsync(
routeResult.Route.BoundingBox,
null,
Windows.UI.Xaml.Controls.Maps.MapAnimationKind.None);

Related

WPF Bing Maps - Zoom to Polyline

I created a WPF Bing map and added polyline I would like set the center and zoom level, which fit polyline. Like map.fitBounds(bounds).
MapPolyline polyline = new MapPolyline();
polyline.Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Blue);
polyline.Locations = new LocationCollection() {
new Location(47.6424, ,-122.3219),
new Location(47.8424,-122.1747),
new Location(47.67856,-122.130994)};
myMap.Children.Add(polyline);
You can get an IEnumerable<Location> from LocationCollection of your polyline and then use an overload of SetView to zoom to the locations. This overload allows you to set a margin as well.
myMap.SetView(polyline.Locations.Cast<Location>(),
new System.Windows.Thickness(0), 0);
Or you can create a LocationRect from LocationCollection of your polyline and then use another overload of SetView to zoom to the rectangle.
myMap.SetView(new LocationRect(polyline.Locations));
Exampel 1 - IEnumerable<Location>
MapPolyline polyline = new MapPolyline();
polyline.Stroke = new SolidColorBrush(Colors.Blue);
polyline.Locations = new LocationCollection() {
new Location(47.6424, -122.3219),
new Location(47.8424,-122.1747),
new Location(47.67856,-122.130994)};
myMap.Children.Add(polyline);
myMap.SetView(polyline.Locations.Cast<Location>(),
new System.Windows.Thickness(0), 0);
Example 2 - LocationRect
MapPolyline polyline = new MapPolyline();
polyline.Stroke = new SolidColorBrush(Colors.Blue);
polyline.Locations = new LocationCollection() {
new Location(47.6424, -122.3219),
new Location(47.8424,-122.1747),
new Location(47.67856,-122.130994)};
myMap.Children.Add(polyline);
myMap.SetView(new LocationRect(polyline.Locations));

GMap.NET how can I make an accurate routing?

So what I am trying to do is showing a route on a winforms application using the GMap.NET nugget.
But the problem I encountered is that it doesn't show the route using the roads. It's literately showing the shortest distances between 2 points, so to make it more accurate I did an extra API call to google maps Directions. So I have some extra points to make it more accurate but still between 2 points its just a straight line, not on roads.
route problem
Here you can see how I am loading the points.
public void ShowRoute(Coordinate[] coordinates)
{
if (coordinates.Length > 1)
{
List<PointLatLng> points = new List<PointLatLng>();
foreach (var cor in coordinates)
{
Coordinate c = geo.ConvertCoordinates(cor);
var corstring = geo.GetAdresByCor(c);
var point = new PointLatLng(Convert.ToDouble(c.Latitude.Replace(".", ",")), Convert.ToDouble(c.Longitude.Replace(".", ",")));
var marker = new GMarkerGoogle(point, GMarkerGoogleType.pink_pushpin);
routeMarkers.Markers.Add(marker);
}
var routes = geo.DirectionsByCor(coordinates);
foreach (var route in routes)
{
foreach (var step in route.Legs[0].Steps)
{
var point = new PointLatLng(step.EndLocation.Latitude, step.EndLocation.Longitude);
points.Add(point);
}
}
var r = new GMapRoute(points, "route");
var routesOverlay = new GMapOverlay("routes");
routesOverlay.Routes.Add(r);
map.Overlays.Add(routeMarkers);
map.Overlays.Add(routesOverlay);
}
}
Here is how my Map is loaded.
private void map_Load(object sender, EventArgs e)
{
GMapProviders.GoogleMap.ApiKey = Properties.Settings.Default.ApiKey;
geo = new GeocoderXY();
map.MouseWheelZoomEnabled = true;
map.DragButton = MouseButtons.Left;
map.MapProvider = GMapProviders.GoogleMap;
lng = 3.7074218;
lat = 51.008157;
GMapMarker marker = new GMarkerGoogle(new PointLatLng(lat, lng), GMarkerGoogleType.blue_pushpin);
searchMarker = marker;
markers.Markers.Add(marker);
markers.Markers.Add(mouseMarker);
map.Position = new NET.PointLatLng(lat, lng);
map.MouseWheelZoomType = NET.MouseWheelZoomType.MousePositionWithoutCenter;
map.MinZoom = 5;
map.MaxZoom = 100;
map.Zoom = 15;
map.ShowCenter = false;
}
I don't see what I'm doing wrong, if someone does see it, please let me know!
Thanks
Still it's not clear why you're doing extra calls.Anyway, to get a route all it's needed a start and end locations and call GetDirections (apparently GoogleMapProvider is obsolete):
GDirections myDirections;
var route = GMapProviders.GoogleMap.GetDirections(out myDirections, start, end, false, false, false, false, false);
then use the directions to draw
var route = new GMapRoute(myDirections.Route, "route");
var routesOverlay = new GMapOverlay("routes");
routesOverlay.Routes.Add(route);
Unfortunately I dont have the VS to test but you got the idea.

Placing an Admob banner at the bottom of screen

I have added a Admob banner to a scene in my app by using the example from: https://forums.xamarin.com/discussion/51779/cocossahrp-and-admob.
The ad displays at the top of the screen but I want to display it at the bottom. I couldn't find any CocosSharp examples but I did find one for cocos2d-x(last post): http://discuss.cocos2d-x.org/t/admob-bottom-placement-tutorial-v3-0/13854/11
I have tried this with my code(see below) but can't get it working. Something I have noticed though is if I change the LayoutParams from 200 to size.Y the ad banner appears in the middle of the screen instead of the top but this happens even when AlignParentBottom or Gravity.Bottom is not used.
Using a RelativeLayout with AddRule:
Android.Graphics.Point size = new Android.Graphics.Point();
prActivity.WindowManager.DefaultDisplay.GetSize(size);
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
size.X,
200);
var bannerAd = new AdView(prActivity);
bannerAd.AdSize = AdSize.SmartBanner;
bannerAd.AdUnitId = "ca-app-pub-3940256099942544/6300978111"; // Test UnitId
bannerAd.LayoutParameters = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
var lcRequestbuilder = new AdRequest.Builder();
bannerAd.LoadAd(lcRequestbuilder.Build());
adParams.AddRule(LayoutRules.AlignParentBottom); // Rule to place ad at the bottom
prActivity.AddContentView(bannerAd, adParams);
ScheduleOnce(t =>
{
bannerAd.BringToFront();
}, 3f);
Using a LinearLayout with Gravity:
Android.Graphics.Point size = new Android.Graphics.Point();
prActivity.WindowManager.DefaultDisplay.GetSize(size);
LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(
size.X,
200);
var bannerAd = new AdView(prActivity);
bannerAd.AdSize = AdSize.SmartBanner;
bannerAd.AdUnitId = "ca-app-pub-3940256099942544/6300978111"; // Test UnitId
bannerAd.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
var lcRequestbuilder = new AdRequest.Builder();
bannerAd.LoadAd(lcRequestbuilder.Build());
adParams.Gravity = GravityFlags.Bottom; // Gravity Flag to place ad at the bottom
prActivity.AddContentView(bannerAd, adParams);
ScheduleOnce(t =>
{
bannerAd.BringToFront();
}, 3f);
I use AdMob Ad in this way in my App and it's work for me(Ad is in the bottom of the screen) :
private static final String AD_UNIT_ID = "";
private AdView adView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(getWindowManager().getDefaultDisplay().getWidth(), (getWindowManager().getDefaultDisplay().getHeight() + getWindowManager().getDefaultDisplay().getHeight()) - 100);
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
adView.setBackgroundColor(Color.TRANSPARENT);
addContentView(this.adView, adParams);
}
getWidth method of Display is deprecated but still i am using it.

Add Pushpin to MapControl in UWP

I'd like to add a Pushpin to a MapControl in a Windows 10 app, but it seems the control is gone since Windows 8.1.
It was as simple as that:
Pushpin locationPushpin = new Pushpin();
locationPushpin.Background = new SolidColorBrush(Colors.Purple);
locationPushpin.Content = "You are here";
locationPushpin.Tag = "locationPushpin";
locationPushpin.Location = watcher.Position.Location;
this.map.Children.Add(locationPushpin);
this.map.SetView(watcher.Position.Location, 18.0);
But Pushpin type is not recognized anymore...
Map control has changed little in UWP - take a look at Windows.UI.Xaml.Controls.Maps namespace.
As for adding Pushpins (POIs), you can do it in couple of ways. For example:
// get position
Geopoint myPoint = new Geopoint(new BasicGeoposition() { Latitude = 51, Longitude = 0 });
//create POI
MapIcon myPOI = new MapIcon { Location = myPoint, NormalizedAnchorPoint = new Point(0.5, 1.0), Title = "My position", ZIndex = 0 };
// add to map and center it
myMap.MapElements.Add(myPOI);
myMap.Center = myPoint;
myMap.ZoomLevel = 10;
For more guidelines visit MSDN.

pin doesn't appear on windows map

I want to add a pin on a certain location on a map for a windows phone 8.0 app.
My code so far is the following:
private async void Button_Click(object sender, RoutedEventArgs e)
{
BasicGeoposition bGeo = new BasicGeoposition();
bGeo.Latitude = 37.4333;
bGeo.Longitude = 24.9167;
Geopoint geoPoint = new Geopoint(bGeo,0);
myMap.ZoomLevel = 13;
myMap.Center = geoPoint;
}
private void AddMapIcon()
{
MapIcon MapIcon1 = new MapIcon();
MapIcon1.Location = new Geopoint(new BasicGeoposition()
{
Latitude = 37.4333,
Longitude = 24.9167
});
MapIcon1.NormalizedAnchorPoint = new Point(2.0, 2.0);
myMap.MapElements.Add(MapIcon1);
}
The map is loading properly, but the pin won't appear. Any ideas on this? Is there any way to do it without using xaml controls for the pin?
This is a general way of adding any UI on map control in windows phone:
We need to create "map layers" and "map overlays" and specify the coordinates where we want to place it. Sample code:
Read the tutorial here
You can add an Image control in the overlay and point its source to the pin image you want to plot. Hope this help
You can try..
BitmapImage myImage1;
myImage1 = new BitmapImage(new Uri("/Assets/Images/pushpin-google-hi.png", UriKind.RelativeOrAbsolute));
var image = new Image();
image.Width = 50;
image.Height = 50;
image.Opacity = 100;
image.Source = myImage1;
var mapOverlay = new MapOverlay();
mapOverlay.Content = image;
mapOverlay.GeoCoordinate = new GeoCoordinate(lats, lons);
var mapLayer = new MapLayer();
mapLayer.Add(mapOverlay);
MyMap.Layers.Add(mapLayer);
MyMap.SetView(new GeoCoordinate(lats, lons), 16);

Categories

Resources