1

im having trouble with getting pushpins on my map. Im using Bing maps and i want to show some coordinates on it. i have followed a tutorial on youtube but still don't manage to make it.

The link is here https://www.youtube.com/watch?v=uVeuib8_MWw&t=2s

So this is what i got! In my class model i got

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Map.Models
{
    public class Locations
    {
        public string latitude { get; set;}
        public string longitude { get; set;}

        public Locations(string latitude, string longitude)
        {
            this.latitude = latitude;
            this.longitude = longitude;
        }
    }
    }

In my controller i have:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Map.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetLocations()
        {
            var locations = new List<Models.Locations>()
            {
                new Models.Locations("12.505353","55.335292"),
                 new Models.Locations("13.505353","55.485292"),
                  new Models.Locations("13.655353","55.665292")

            };
            return Json(locations, JsonRequestBehavior.AllowGet);
        }


    }
}

And at the end its the view. Here is the map aswell and the pushpins.

@{
    ViewBag.Title = "Home Page";
}
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
<script type="text/javascript">

    $(document).ready(function () {

        var map = null;
        function LoadMap() {
            map = new Microsoft.Maps.Map(
                document.getElementById('myMap'),
                {
                    credentials: "Bing map key"
                });

    }
        LoadMap();

        $('#btnShowLocations').click(function () {
            var url = "/Home/GetLocations";
            $.getJSON(url, null, function (data) {
                $.each(data, function (index, LocationData) {

                    var pushpin = new Microsoft.Maps.pushpin(map.getCenter(), null);
                    pushpin.setLocation(new Microsoft.Maps.Location(
                        LocationData.latitude,
                        LocationData.longitude));

                    map.entities.push(pushpin);
                    map.setView({
                        zoom: 4, center: new Microsoft.Maps.Location(23.505353, 78.485292)
                    });

                });

            });
        });
    });
</script>

<h2>Bing Map integration in ASP.NET</h2>
<input type="button" id="btnShowLocations" value ="Show All Locations" />

<div id="myMap" style="position:relative; width:600px; height:600px;">


</div>

The map is working and i get no errors. My problem is that when i press the button nothing happens. What i want is that when the button is pressed there should be 3 pushpins on the given coordinates.

Thanks so very much for reading! i hope i can get it to work!

1 Answer 1

3

A few issues and recommendations:

  • The main issue is that your latitude and longitude values are strings and are never parsed as floats/numbers. As such the map is getting string values for locations when it is expecting numbers.
  • Your code is using Bing Maps V7 which was replaced by V8 a while ago. V7 is nearing end of life and will be turned off at the end of June. This is the new map script URL to V8: http://www.bing.com/api/maps/mapcontrol
  • document.ready will fire long before the map script will load as it loads asynchronously. In fact, document.ready sometimes will fire before the whole page is loaded which means your map div might not event be available. I suggest using the callback parameter of the map script UR: for example: http://www.bing.com/api/maps/mapcontrol?callback=LoadMap
  • You are setting the view of the map in a loop, this should work, but will cause a lot of extra refreshes for nothing which will cost you in load performance.
  • A couple of recommendations:
    • add your pushpins to an array then add the array to the map. This will reduce the number of refreshes needed.
    • move your scripts to the bottom of the page and call the map script URL last since it loads asynchronously. When the map script is cached it and you press "refresh", the callback gets called instantly and often before the rest of your code is loaded. Moving this line of code to the bottom allows your page to load the fastest it can.

Here is some suggested modifications to your code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Map.Models
{
    public class Locations
    {
        public double latitude { get; set;}
        public double longitude { get; set;}

        public Locations(double latitude, double longitude)
        {
            this.latitude = latitude;
            this.longitude = longitude;
        }
    }
}

Your controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Map.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetLocations()
        {
            var locations = new List<Models.Locations>()
            {
                new Models.Locations(12.505353,55.335292),
                 new Models.Locations(13.505353,55.485292),
                  new Models.Locations(13.655353,55.665292)
            };
            return Json(locations, JsonRequestBehavior.AllowGet);
        }
    }
}

Your view:

@{
    ViewBag.Title = "Home Page";
}

<h2>Bing Map integration in ASP.NET</h2>
<input type="button" id="btnShowLocations" value ="Show All Locations" />

<div id="myMap" style="position:relative; width:600px; height:600px;"></div>

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
var map = null;

function LoadMap() {
    map = new Microsoft.Maps.Map('#myMap', {
            credentials: "Bing map key"
        });
}

$('#btnShowLocations').click(function () {
    var url = "/Home/GetLocations";
    $.getJSON(url, null, function (data) {
        var pins = [];

        $.each(data, function (index, LocationData) {
            var pushpin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(
                LocationData.latitude,
                LocationData.longitude));

            pins.push(pushpin);
        });

        map.entities.push(pins);

        map.setView({
            zoom: 4, center: new Microsoft.Maps.Location(23.505353, 78.485292)
        });
    });
});
</script>
<script type="text/javascript" src="https://www.bing.com/api/maps/mapcontrol?callback=LoadMap" async defer></script>
Sign up to request clarification or add additional context in comments.

7 Comments

When i use your view code the Map disappears. I don't get the map script to work "<script type="text/javascript" src="bing.com/api/maps/…" async defer></script>" Thanks so much for helping me, i have been stuck with this for a long time!
Had the wrong callback function name in the URL. Corrected code sample.
i got the map to work now! But when i press the button nothing happens. I dont get any pushpins.
Remove setLocation and pass the location in when creating the pushpin (code sample updated). Try using break points and debugging to see where the issue is. Is there any errors?
Yes! i found an error! after i pressed the button i get this error code in the console for chrome Uncaught TypeError: Microsoft.Maps.pushpin is not a constructor at Object.<anonymous> (Index:65) at Function.each (jquery-1.10.2.js:671) at Object.success (Index:64) at fire (jquery-1.10.2.js:3062) at Object.fireWith [as resolveWith] (jquery-1.10.2.js:3174) at done (jquery-1.10.2.js:8249) at XMLHttpRequest.callback (jquery-1.10.2.js:8792) Here is a link on the error message ibb.co/dptyGQ
|

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.