Skip to content

Instantly share code, notes, and snippets.

@suchithm
Last active September 3, 2015 12:30
Show Gist options
  • Save suchithm/01d240225127d521815b to your computer and use it in GitHub Desktop.
Save suchithm/01d240225127d521815b to your computer and use it in GitHub Desktop.
//Code snippet to mark source and destination points
Location SourceLocation{get;set;}
Location DestinationLocation{get;set;}
//mark source point
strGeoCodeURL =string.Format ("https://maps.googleapis.com/maps/api/geocode/json?address={0}&key={1}",strSource,"ApiKeygeoeshere");
string strHttpResponse= await FnHttpRequest (strGeoCodeURL);
GeoCodeJSONClass objGeoCodeJSONClass = JsonConvert.DeserializeObject<GeoCodeJSONClass> (strHttpResponse);
//find GeoCodeJSONClass below in this page
if(objGeoCodeJSONClass!=null && objGeoCodeJSONClass.status=="OK")
{
SourceLocation=new Location(){lat=objGeoCodeJSONClass.results[0].geometry.location.lat,lng=objGeoCodeJSONClass.results[0].geometry.location.lng};
MarkOnMap (SourceLocation,"Start",UIImage.FromBundle("Images/MarkerSource"),false);
}
//mark destination point
strGeoCodeURL =string.Format ("https://maps.googleapis.com/maps/api/geocode/json?address={0}&key={1}",strDestination,"apiKeygeoeshere");
string strHttpResponse = await FnHttpRequest ( strGeoCodeURL );
var objGeoCodeJSONClass = JsonConvert.DeserializeObject<GeoCodeJSONClass> (strHttpResponse);
if ( objGeoCodeJSONClass != null && objGeoCodeJSONClass.status == "OK" )
{
DestinationLocation = new Location () { lat = objGeoCodeJSONClass.results [0].geometry.location.lat, lng = objGeoCodeJSONClass.results [0].geometry.location.lng };
MarkOnMap ( DestinationLocation , "End" , UIImage.FromBundle ( "Images/MarkerDest" ) , false );
}
void MarkOnMap(Location location,string strTitle,UIImage imgIcon,bool isFlat)
{
var marker = new Marker ();
marker.Flat = isFlat;
marker.Position = new CLLocationCoordinate2D ( location.lat , location.lng );
marker.Map = mapView;
marker.Title = strTitle;
marker.Icon=imgIcon;
marker.Draggable=false;
marker.AppearAnimation=MarkerAnimation.Pop;
}
//HttpWebRequest common method
static async Task<string> FnHttpRequest(string strURL)
{
WebClient client = new WebClient ();
string strResult;
try
{
strResult=await client.DownloadStringTaskAsync (new Uri(strURL));
}
catch
{
strResult="Exception";
}
finally
{
client.Dispose ();
client = null;
}
return strResult;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment