简体   繁体   English

从Razor模型加载多个Bing Maps引脚

[英]Load multiple Bing Maps pins from Razor model

I'm new at using javascript and ASP.Net MVC 4. But i have tried to add multiple pins in the bing map, from the Razor @Model. 我是使用javascript和ASP.Net MVC 4的新手。但是我试图在Razor @Model的bing map中添加多个引脚。 i have tried: 我努力了:

@model IEnumerable<Foundation3SinglePageRWD.Models.Locations>
            <div id="map" style="height: 800px; width: 100%"></div>

    <script type="text/javascript">
    $(function () {
        var map = null;
        var location = null;

        function loadMap() {
            // initialize the map
            map = new Microsoft.Maps.Map(document.getElementById('map'), {
                credentials: "My Bing Map Key",
                enableSearchLogo: false
            });

        }

        function showPosition(position) {
                    function showPosition(position) {
            //display position
            var location = position.coords;
            map.setView({ zoom: 10, center: new Microsoft.Maps.Location(location.latitude, location.longitude) });
            //var pushpinOptions = { icon: virtualPath + '/Content/images/foundation/orbit/Push.png' };
            var test = '@model';
            alert(test);
            var pushpin = new Microsoft.Maps.Pushpin(map.getCenter(), null);
          for (var i = 0; i < test.l.length; i++) {
              map.entities.push(pushpin);
              pushpin.setLocation(new Microsoft.Maps.Location(test.latitude, test.longitude));
          };

        }

        }
        function positionError(position) {
            alert("Error getting position. Code: " + position.code);
        }
        loadMap();
        navigator.geolocation.getCurrentPosition(showPosition, positionError);

    });

</script>

The Controller: 控制者:

   public ActionResult Index()
        {
            List<Models.Locations> loc = new List<Models.Locations>();
            Models.Locations temp = new Models.Locations("55.473746", "8.447411");
            loc.Add(temp);
            Models.Locations temp1 = new Models.Locations("55.504991", "8.443698");
            loc.Add(temp1);
            Models.Locations temp2 = new Models.Locations("55.468283", "8.438");
            loc.Add(temp2);
            Models.Locations temp3 = new Models.Locations("55.498978", "8.40002");
            loc.Add(temp3);
            return View(loc);
        }

and finally the Model: 最后模型:

  public class Locations
    {
        public string latitude { get; set; }
        public string longitude { get; set; }
        public List<Locations> Loca { get; set; }
        public Locations(string latitude, string longitude)
        {
            this.latitude = latitude;
            this.longitude = longitude;
        }
    }

I believe that you are facing an issue in converting the Model to javascript object. 我相信您在将模型转换为javascript对象时遇到了问题。

Below I separated the Index view from the part which actually fetches the data 下面我将Index视图与实际获取数据的部分分开

  1. Index returns just your page which would then make an ajax call for fetching locations. Index只返回您的页面,然后进行ajax调用以获取位置。

  2. GetLocations returns a JSON object array of locations to be used for rendering positions on Bing Maps GetLocations返回一个JSON对象数组,用于在Bing Maps上渲染位置

Changes to Controller 对控制器的更改

    public ActionResult Index()
    {
         return View();
    }


    public ActionResult GetLocations()
    {
        List<Models.Locations> locations = new List<Models.Locations>()
        {
            new Models.Locations("55.473746", "8.447411") ,
            new Models.Locations("55.504991", "8.443698"),
            new Models.Locations("55.468283", "8.438"),
            new Models.Locations("55.468283", "8.438"),
            new Models.Locations("55.468283", "8.438"),
            new Models.Locations("55.498978", "8.40002")
        }
        return JsonResult(locations);
    }

Javascript Changes Javascript更改

Changed showPosition which now makes an ajax request fetching JSON location list and pushing it onto the map. 改变了showPosition ,它现在使ajax请求获取JSON位置列表并将其推送到地图上。 Note : You might have to refactor rest of your javascript just a bit. 注意: 您可能需要稍微重构其余的javascript。

    function showPosition(position) 
    {
         //display position
          var location = position.coords;
           map.setView({ 
               zoom: 10, 
               center: new Microsoft.Maps.Location(location.latitude, 
                                                   location.longitude) 
           });

           $.ajax({
                url  : 'getlocations' , 
                type : 'json'

             }).done(function(locationsArray){

            alert(locationsArray);
            var pushpin = new Microsoft.Maps.Pushpin(map.getCenter(), null);

            $.each(locationsArray, 
                   function(index,location)
                   {
                        map.entities.push(pushpin);
                        pushpin.setLocation(new Microsoft.Maps.Location(
                            location.latitude, 
                            location.longitude));
                   });
              };
    }

I an not sure, about bing maps. 我不确定,关于bing地图。 But i have implemented same with google maps. 但我已经实现了相同的谷歌地图。 may be this will give you some idea http://www.harshabopuri.com/onclick-dynamic-google-maps-in-website 也许这会给你一些想法http://www.harshabopuri.com/onclick-dynamic-google-maps-in-website

If you use JavaScriptModel ( http://jsm.codeplex.com ) you could do it the following way: 如果您使用JavaScriptModel( http://jsm.codeplex.com ),您可以通过以下方式执行此操作:

Your controller code: 您的控制器代码:

public ActionResult Index()
{
    this.AddJavaScriptVariable("LocationsVariableInJavaScript", GetLocationList());
    this.AddJavaScriptFunction("YourMapInitFunctionInJavaScript");
    return View();
}

private GetLocationList()
{
    List<Models.Locations> loc = new List<Models.Locations>();
    Models.Locations temp = new Models.Locations("55.473746", "8.447411");
    loc.Add(temp);
    Models.Locations temp1 = new Models.Locations("55.504991", "8.443698");
    loc.Add(temp1);
    Models.Locations temp2 = new Models.Locations("55.468283", "8.438");
    loc.Add(temp2);
    Models.Locations temp3 = new Models.Locations("55.498978", "8.40002");
    loc.Add(temp3);
    return loc
}

In your javascript file just iterate over locations (by using "LocationsVariableInJavaScript") and add the pushpins. 在您的javascript文件中,只需遍历位置(通过使用“LocationsVariableInJavaScript”)并添加图钉。

This way you separate javascript data from javascript logic and you don't have any javascript in the view. 这样您就可以将javascript数据与javascript逻辑分开,并且视图中没有任何javascript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM