Skip to content

Instantly share code, notes, and snippets.

@glennstephens
Created September 16, 2015 22:48
Show Gist options
  • Save glennstephens/a122b3b93057a51f572c to your computer and use it in GitHub Desktop.
Save glennstephens/a122b3b93057a51f572c to your computer and use it in GitHub Desktop.
MapBox Static Images Implementation. Useful for Devices without Google Maps, like the Kindle Fire devices or for offline Map Display
public enum QualityFormat
{
iOSRetina,
PNG32,
PNG64,
PNG128,
PNG256,
JPG70,
JPG80,
JPG90
}
public class QualityFormatHelper
{
public static string EmbeddedFormat(QualityFormat format)
{
switch (format)
{
case QualityFormat.iOSRetina:
return "@2x.png";
case QualityFormat.PNG32:
return "png32";
case QualityFormat.PNG64:
return "png64";
case QualityFormat.PNG128:
return "png128";
case QualityFormat.PNG256:
return "png256";
case QualityFormat.JPG70:
return "jpg70";
case QualityFormat.JPG80:
return "jpg80";
case QualityFormat.JPG90:
return "jpg90";
}
return "";
}
}
public enum MarkerDisplayType
{
PinS,
PinM,
PinL
}
public class Marker
{
public double Latitude { get; set; } = 0;
public double Longitude { get; set; } = 0;
public MarkerDisplayType DisplayType { get; set; } = MarkerDisplayType.PinM;
public KnownMarkerLabel Label { get; set; } = KnownMarkerLabel.None;
public string Color { get; set; } = "FF0000";
public string CustomMarkerUrl { get; set; } = null;
private string EncodedCustomMarkerUrl => System.Net.WebUtility.UrlEncode(CustomMarkerUrl);
private string DisplayTypeHttpValue
{
get
{
switch (DisplayType)
{
case MarkerDisplayType.PinS:
default:
return "pin-s";
case MarkerDisplayType.PinM:
return "pin-m";
case MarkerDisplayType.PinL:
return "pin-l";
}
}
}
private string MarkerLabelType => KnownMarkerLabelHelper.GetUrlValue(Label);
public string UrlString
{
get
{
if (string.IsNullOrEmpty(CustomMarkerUrl))
return $"{DisplayTypeHttpValue}{MarkerLabelType}+{Color}({Longitude},{Latitude})";
else
return $"url-{EncodedCustomMarkerUrl}({Longitude},{Latitude})";
}
}
}
public enum KnownMarkerLabel
{
aerialway, airfield, airport, alcohol_shop, america_football, art_gallery, bakery, bank, bar,
baseball, basketball, beer, bicycle, building, bus, cafe, camera, campsite, car, cemetery,
chemist, cinema, circle_stroked, circle, city, clothing_store, college, commercial, cricket,
cross, dam, danger, dentist, disability, dog_park, embassy, emergency_telephone, entrance,
farm, fast_food, ferry, fire_station, fuel, garden, gift, golf, grocery, hairdresser,
harbor, heart, heliport, hospital, ice_cream, industrial, land_use, laundry, library,
lighthouse, lodging, logging, london_underground, marker_stroked, marker, minefield,
mobilephone, monument, museum, music, oil_well, park2, park, parking_garage, parking,
pharmacy, pitch, place_of_worship, playground, police, polling_place, post, prison,
rail_above, rail_light, rail_metro, rail_underground, rail, religious_christian,
religious_jewish, religious_muslim, restaurant, roadblock, rocket, school, scooter,
shop, skiing, slaughterhouse, soccer, square_stroked, square, star_stroked, star,
suitcase, swimming, telephone, tennis, theatre, toilets, town_hall, town, triangle_stroked,
triangle, village, warehouse, waste_basket, water, wetland, zoo, None
}
public class KnownMarkerLabelHelper
{
public static string GetUrlValue(KnownMarkerLabel label)
{
if (label == KnownMarkerLabel.None)
return "";
else
return "-" + label.ToString().Replace("_", "-");
}
}
// If you are looking for an example on how to use it, do something like the following:
var mb = new MapBox {
CenterLatitude = 40.783435,
CenterLongitude = -73.966249,
AccessToken = "<Your Api key from MapBox>",
Zoom = 18,
Markers = new List<Marker> () {
new Marker () {
Color = "FF0000",
Longitude = 153.133954,
Latitude = -73.966249,
DisplayType = MarkerDisplayType.PinL,
Label = KnownMarkerLabel.camera
}
}
};
// Then use the mb.Url to load into a Xamarin.Forms Image, a Android ImageView or iOS UIImage
@glennstephens
Copy link
Author

This is a small snippet for accessing MapBox so that images of the maps can be stored downloaded and then stored for later offline access. It is also useful for devices where you don't have a Google Maps implementation like the Kindle Fire devices. In this way you can use something like a Xamarin.Forms image to display a readonly map instead if you are displaying a location.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment