public List<double> GoogleGeoCode(string address) { address = "Stockholm"; string url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address="; dynamic googleResults = new Uri(url + address).GetDynamicJsonObject(); var cordinates = new List<double>(); foreach (var result in googleResults.results) { cordinates.Add(result.geometry.location.lng); cordinates.Add(result.geometry.location.lat); } return cordinates; } Now, I want two use the values in the list in my Ajax:
$(function() { $('#ajax').click(function() { $.ajax({ url: '@Url.Action("GoogleGeoCode", "Home")', context: document.body }) .done(function(serverdata) { data = serverdata; $.each(data, function(i, item) { var marker = new google.maps.Marker({ 'position': new google.maps.LatLng(item[0], item.GeoLat[1]), <-- I thought Item would contain my list and I could use its index 'map': map, 'title': item.PlaceName, 'Catch': item.Catch }); }); }); }); Im a beginner with Ajax i would really appreciate a step by step explanation on how it works if someone got the time. Thanks!
Updat: New class:
public class LongLatModel { public double Latitude { get; set; } public double Longitude { get; set; } } Controller:
public LongLatModel GoogleGeoCode(string address) { address = "Stockholm"; string url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address="; dynamic googleResults = new Uri(url + address).GetDynamicJsonObject(); var cordinates = new List<double>(); var longlat = new LongLatModel(); foreach (var result in googleResults.results) { longlat.Latitude = result.geometry.location.lng; longlat.Longitude = result.geometry.location.lat; } return longlat; } Ajax:
$(function() { $('#ajax').click(function() { $.ajax({ // in a real scenario, use data-attrs on the html // rather than linking directly to the url url: '@Url.Action("GoogleGeoCode", "Home")', context: document.body }) .done(function(serverdata) { data = serverdata; $.each(data, function(i, item) { var marker = new google.maps.Marker({ 'position': new google.maps.LatLng(data.longitude, data.latitude), 'map': map, 'title': item.PlaceName, 'Catch': item.Catch }); }); }); }); EDIT:
Ok, so I managed to get the right info into the AJAX (i think..) However, I have trouble relising how to place these two values where they are supposed to be.

How can i loop throgh and place them in the right place?
$.each(data, function (i, item) {
var marker = new google.maps.Marker({ 'position': new google.maps.LatLng(LONG, LAT),
});