0

I create application ASP.NET Core MVC 2 for show markers on map. I create Model and Controller.

Model:

    public class MarkersModel
{

    public string title { get; set; }

    public double lat { get; set; }

    public double lng { get; set; }

}

Controller:

    public class MarkersController : Controller
{
    public IActionResult Index()
    {
        return View("Markers");
    }

    public IActionResult GetMarkers()
    {
        //landmarks
        var model1 = new MarkersModel {lat = 53.43382, lng = 14.55559, title = "Galaxy"};
        var model2 = new MarkersModel { lat = 53.42800, lng = 14.55124, title = "Kaskada" };

        //buildings
        var model3 = new MarkersModel { lat = 53.43263, lng = 14.55436, title = "Zig-Zag" };
        var model4 = new MarkersModel { lat = 53.43241, lng = 14.55568, title = "Baltica" };

        //stops
        var model5 = new MarkersModel {lat = 53.43269, lng = 14.54787, title = "Plac Grunwaldzki" };
        var model6 = new MarkersModel { lat = 53.43186, lng = 14.55485, title = "Plac Rodła"};

        //others
        var model7 = new MarkersModel { lat = 53.43133, lng = 14.55519, title = "KFC" };
        var model8 = new MarkersModel { lat = 53.43172, lng = 14.55384, title = "Kurczak z rożna"};

        //MarkersModel.
        return Json(new {model1, model2, model3, model4, model5, model6, model7, model8 });

        //return Json(new { Response = model1, model2, model3, model4});

    }
}

Ajax I'm get data from the controller and place in variable "response" :

function getData() {
//alert(data["name"]);
$.ajax({
    url: '/Markers/GetMarkers', //Controller/Akcja

    type: 'POST',
    dataType: "json",
    data: JSON.stringify(),
    contentType: 'application/json; charset=utf-8',

    success: function(response) {
        initMap(response);
    },

    error: function(error) {
        alert('error');
    }

in function initMap show Object JSON console.log(data):

function initMap(data) {

console.log(data);

    var map = new google.maps.Map(
    document.getElementById('map'),
    { zoom: 10, center: { lat: 53.42894, lng: 14.55302 } });

// test, do not work
var myMarkers = $.parseJSON(data);

}

How conver JSON to array Javascript or how show add markers on map ?

student_it
  • 79
  • 7

1 Answers1

1

This should fix the problem:

//Parse the array
var jsonArrayParsed = JSON.parse(yourJsonArray);

// Empty js array
var jsArray = [];

// Loop json array and add all the items to a regular js array.
for (var item in jsonArray){
  if (jsonArray.hasOwnProperty(item)){
    // add to your js array
    jsArray.push(item);
  }
}
yaKay
  • 115
  • 13
  • So I have to create `var arrayJs = new Array();` and in loop if add item to arrayJs? `if (jsonArray.hasOwnProperty(item)){` `arrayJs = item;` `}` – student_it Dec 14 '18 at 21:28
  • One more thing, my JSON have data for example : `Object model1: {title: "Galaxy", lat: 53.43382, lng: 14.55559}`and use your help code and I get in array : `Array(8) 0: "model1" 1: "model2" 2: "model3"` How change JASON to array that values in array will for example how in JSON ? – student_it Dec 15 '18 at 16:37