Skip to content

Instantly share code, notes, and snippets.

@lobrien
Last active November 18, 2016 19:52
Show Gist options
  • Save lobrien/85a3023a670aa930995d77c3c58d5960 to your computer and use it in GitHub Desktop.
Save lobrien/85a3023a670aa930995d77c3c58d5960 to your computer and use it in GitHub Desktop.
---
uti: com.xamarin.workbook
platforms:
- iOS
---
```csharp
using MapKit;
```
```csharp
var region = new MKCoordinateRegion();
region.Center.Latitude = 37.77;
region.Center.Longitude = -122.42;
region.Span.LatitudeDelta = 0.2;
region.Span.LongitudeDelta = 0.2;
```
```csharp
var map = new MKMapView{
ShowsUserLocation = true,
ZoomEnabled = true,
ScrollEnabled = true,
Region = region
}
```
```csharp
map.Frame = RootViewController.View.Bounds;
RootViewController.View.AddSubview(map);
```
```csharp
var img1 = UIImage.FromFile("bridge.jpg");
```
```csharp
var img2 = UIImage.FromFile("park.png")
```
```csharp
class MyMapDelegate : MKMapViewDelegate
{
static string ID = "MyAnnotationID";
public Dictionary<IMKAnnotation, UIImage> ImageForAnnotation { get; }
public MyMapDelegate()
{
ImageForAnnotation = new Dictionary<IMKAnnotation, UIImage>();
}
override public MKAnnotationView GetViewForAnnotation (MKMapView mapView, IMKAnnotation annotation)
{
var annotationView = mapView.DequeueReusableAnnotation(ID);
if (annotationView == null)
{
annotationView = new MKAnnotationView(annotation, ID);
}
else
{
annotationView.Annotation = annotation;
}
annotationView.CanShowCallout = true;
if(ImageForAnnotation.ContainsKey(annotation))
{
annotationView.Image = ImageForAnnotation[annotation];
}
return annotationView;
}
}
```
```csharp
var ann1 = new MKPointAnnotation{
Coordinate = new CoreLocation.CLLocationCoordinate2D(37.82, -122.48)
};
```
```csharp
var ann2 = new MKPointAnnotation{
Coordinate = new CoreLocation.CLLocationCoordinate2D(37.77, -122.48)
}
```
```csharp
var myDel = new MyMapDelegate();
myDel.ImageForAnnotation[ann1] = img1;
myDel.ImageForAnnotation[ann2] = img2;
map.Delegate = myDel;
map.AddAnnotation(ann1);
map.AddAnnotation(ann2);
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment