1

I am using a javascript based map for marine traffic and while it works great I need to be able to pass from .net the latitude and longitude of a ship which I get from their api. I will have the data in my model already.

Would it be better making this a view component. Or how would I achieve this.

<div class="tab-pane fade" id="custom-tabs-one-map" role="tabpanel" aria-labelledby="custom-tabs-one-map-tab">
<script type="text/javascript">
    width = '100%';     // the width of the embedded map in pixels or percentage
    height = '600';     // the height of the embedded map in pixels or percentage
    border = '1';       // the width of the border around the map (zero means no border)
    shownames = 'false';    // to display ship names on the map (true or false)
    latitude = '37.4460';   // the latitude of the center of the map, in decimal degrees
    longitude = '24.9467';  // the longitude of the center of the map, in decimal degrees
    zoom = '9';     // the zoom level of the map (values between 2 and 17)
    maptype = '1';      // use 0 for Normal Map, 1 for Satellite
    trackvessel = '0';  // MMSI of a vessel (note: vessel will be displayed only if within range of the system) - overrides "zoom" option
    fleet = '';     // the registered email address of a user-defined fleet (user's default fleet is used)
</script>
<script type="text/javascript" src="//www.marinetraffic.com/js/embed.js"></script>

My Model is just

public  class Vessels {

    public int Id { get; set; }
    public string Name { get; set; }
    public string Flag { get; set; }
    public decimal Lat { get; set; }
    public decimal Long { get; set; }
}

Thanks in advance

3
  • 1
    var model = @Html.Raw(Json.Encode(Model)); Commented Jul 13, 2020 at 17:52
  • 1
    Sweet and thats just within the javascript I will get access to that and how would I adjust the javasript to work from the properties then an expanded answer would be great @LDS Commented Jul 13, 2020 at 17:58
  • var jmodel = JSON.parse(model); // will give json Commented Jul 13, 2020 at 18:09

1 Answer 1

1

In this example, we build a mvc project to pass the latitude and longitude of a ship from controller.

    public IActionResult Index()
    {
        //get values from api, here we just use new Vessel instead
        Vessel vessle = new Vessel {
            Id = 1001,
            Name = "Hope",
            Flag = "",
            Lat = (decimal)37.4460,
            Long = (decimal)24.9467
        };

        return View(vessle);
    }

Here's View. You can directly access by @Mode.

@model Vessel

<div class="tab-pane fade" id="custom-tabs-one-map" role="tabpanel" aria-labelledby="custom-tabs-one-map-tab">
    <script type="text/javascript">
    width = '100%';     // the width of the embedded map in pixels or percentage
    height = '600';     // the height of the embedded map in pixels or percentage
    border = '1';       // the width of the border around the map (zero means no border)
    shownames = 'false';    // to display ship names on the map (true or false)
    latitude = @Model.Lat;   // the latitude of the center of the map, in decimal degrees
    longitude = @Model.Long;  // the longitude of the center of the map, in decimal degrees
    zoom = '9';     // the zoom level of the map (values between 2 and 17)
    maptype = '1';      // use 0 for Normal Map, 1 for Satellite
    trackvessel = '0';  // MMSI of a vessel (note: vessel will be displayed only if within range of the system) - overrides "zoom" option
    fleet = '';     // the registered email address of a user-defined fleet (user's default fleet is used)
    </script>
<script type="text/javascript" src="//www.marinetraffic.com/js/embed.js"></script>

Test

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks at least I no I could do this in a view component that is great.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.