I have a requirement to get latitude and longitude from address using C# or Javascript, any solution will work. So, how can I get them using address?
Basically, I need to convert the address into Lat / Long and show it in Google maps in ASP.NET MVC web-application, so javascript solution will also work.
Note: I don't have lat/long, just address of the user.
You can use Geocoding API of Google maps, here is the url format for it
https://maps.googleapis.com/maps/api/geocode/json?key=API-KEY&address=YourAddress&sensor=true
Now, suppose you need to convert "Address into Lat/long using C#", here is the method for it in C#
var address = "SomeAddress";
string url = "https://maps.googleapis.com/maps/api/geocode/json?key=APIKEY&address=" + address + "&sensor=true";
var client = new RestClient(url);
var request = new RestRequest(Method.GET);
request.Timeout = 1000000;
request.ReadWriteTimeout = 1000000;
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/json");
var response = client.Execute(request);
JavaScriptSerializer oJS = new JavaScriptSerializer();
oJS.MaxJsonLength = int.MaxValue;
var oRootObject = oJS.Deserialize<MapsResponse.Rootobject>(response.Content);
var markers = "[";
markers += "{";
markers += string.Format("'title': '{0}',", "Title Of marker");
markers += string.Format("'lat': '{0}',", oRootObject.results[0].geometry.location.lat);
markers += string.Format("'lng': '{0}',", oRootObject.results[0].geometry.location.lng);
markers += string.Format("'description': '{0}'", "Description Here");
markers += "},";
markers += "]";
Your MapsResponse class would be as below
public class MapsResponse
{
public class Rootobject
{
public Result[] results { get; set; }
public string status { get; set; }
}
public class Result
{
public Address_Components[] address_components { get; set; }
public string formatted_address { get; set; }
public Geometry geometry { get; set; }
public string place_id { get; set; }
public string[] types { get; set; }
}
public class Geometry
{
public Location location { get; set; }
public string location_type { get; set; }
public Viewport viewport { get; set; }
}
public class Location
{
public float lat { get; set; }
public float lng { get; set; }
}
public class Viewport
{
public Northeast northeast { get; set; }
public Southwest southwest { get; set; }
}
public class Northeast
{
public float lat { get; set; }
public float lng { get; set; }
}
public class Southwest
{
public float lat { get; set; }
public float lng { get; set; }
}
public class Address_Components
{
public string long_name { get; set; }
public string short_name { get; set; }
public string[] types { get; set; }
}
}
In the above method string "markers" is a JSON string which contains list of address converted in Google map markers which has Lat/Long , Title and description data.
Now you can use "markers" JSON in your HTML view and use Javascript like below
<div id="dvMap" style="width: 500px; height: 500px">
</div>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YourKEY"></script>
<script type="text/javascript">
//Get Markers from server in some way
var markers = "MarkersJSON";
function loadMap() {
var mapOptions = {
center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
zoom:15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var infoWindow = new google.maps.InfoWindow();
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
for (i = 0; i < markers.length; i++) {
var data = markers[i]
var myLatlng = new google.maps.LatLng(data.lat, data.lng);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: data.title
});
(function (marker, data) {
google.maps.event.addListener(marker, "click", function (e) {
infoWindow.setContent(data.description);
infoWindow.open(map, marker);
});
})(marker, data);
}
}
</script>
You need to provide "markers" JSON here by calling Ajax or in some other way, and do not forget to use your API-Key in the javascript also.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly