0
xamarin iosでmkMapを使用してルート行を描画したい。私のコードは正しく動作していますが、ポイント間のルートラインは表示されません。私のコードは 下に与えられている私の最初の画像は、出発注釈点を示し、第二の画像は、注釈点を終了xamarin iosでMkMapを使用してルート行が表示されない
のMapViewコード示す:
private MKMapView _map;
private MapDelegate _mapDelegate;
public QiblaCompassVC (IntPtr handle) : base (handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
_map = new MKMapView(mapsView.Bounds)
{
MapType = MKMapType.Standard,
ShowsUserLocation = true,
ZoomEnabled = true,
ScrollEnabled = true
};
//_map = new MKMapView(mapsView.Bounds);
// _map.ShowsUserLocation = true;
_mapDelegate = new MapDelegate();
_map.Delegate = _mapDelegate;
//mapsView.Add(_map);
View = _map;
var target = new CLLocationCoordinate2D(30.3753, 69.3451);
var viewPoint = new CLLocationCoordinate2D(21.3891, 39.8579);
var annotation = new mapAnnotation(new CLLocationCoordinate2D(30.3753, 69.3451), "Pakistan", "Countery of love");
_map.AddAnnotation(annotation);
var annotation1 = new mapAnnotation(new CLLocationCoordinate2D(21.3891, 39.8579), "Makka", "Allah home");
_map.AddAnnotation(annotation1);
var camera = MKMapCamera.CameraLookingAtCenterCoordinate(target, viewPoint, 500);
_map.Camera = camera;
createRoute();
//CLLocationCoordinate2D coords = new CLLocationCoordinate2D(30.3753, 69.3451);
//MKCoordinateSpan span = new MKCoordinateSpan(MilesToLatitudeDegrees(20), MilesToLongitudeDegrees(20, coords.Latitude));
//_map.Region = new MKCoordinateRegion(coords, span);
}
public void createRoute()
{
var dict = new NSDictionary();
var orignPlaceMark = new MKPlacemark(new CLLocationCoordinate2D(30.3753, 69.3451), dict);
var sourceItem = new MKMapItem(orignPlaceMark);
//End at Xamarin Cambridge Office
var destPlaceMark = new MKPlacemark(new CLLocationCoordinate2D(21.3891, 39.8579), dict);
var destItem = new MKMapItem(destPlaceMark);
var request = new MKDirectionsRequest
{
Source = sourceItem,
Destination = destItem,
RequestsAlternateRoutes = true,
};
var directions = new MKDirections(request);
directions.CalculateDirections((response, error) =>
{
if (error != null)
{
Console.WriteLine(error.LocalizedDescription);
}
else
{
//Add each Polyline from route to map as overlay
foreach (var route in response.Routes)
{
_map.AddOverlay(route.Polyline);
}
}
});
}
MapDelegateコード:
class MapDelegate : MKMapViewDelegate
{
public override MKOverlayRenderer OverlayRenderer(MKMapView mapView, IMKOverlay overlay)
{
if (overlay is MKPolyline)
{
var route = (MKPolyline)overlay;
var renderer = new MKPolylineRenderer(route) { StrokeColor = UIColor.Blue };
return renderer;
}
return null;
}
public override MKOverlayView GetViewForOverlay(MKMapView mapView, IMKOverlay overlay)
{
if (overlay is MKPolyline)
{
// return a view for the polygon
MKPolyline l_polyline = overlay as MKPolyline;
MKPolylineView l_polylineView = new MKPolylineView(l_polyline);
MKPolylineRenderer l_polylineRenderer = new MKPolylineRenderer(l_polyline);
l_polylineView.FillColor = UIColor.Blue;
l_polylineView.StrokeColor = UIColor.Red;
return l_polylineView;
}
return null;
}
}