MVC5上的Google Map API

时间:2015-10-08 20:18:30

标签: google-maps-api-3 asp.net-mvc-5

我在尝试为地图上的多个地址生成多个标记时遇到问题。所以基本上,我从数据库中提取地址并让GoogleLocationService()检测它的坐标。虽然我不确定如何为我的VIEW(.CSHTML)传递多个latitue和经度。 我在MVC上还是新手,所以我很难将值从控制器传递到视图。有什么建议吗?

在我的代码(控制器)上:

   public ActionResult Index() {

        VerifyUserModel user = new VerifyUserModel();
        user.UserName = "Kenneth Baleda";
        user.AccountLevel = "MGR";

        foreach (string w in a.Companies.Where(x => x.IsActive == "Active").Where(x => x.CompanyName != "tbd").Where(x => x.FullCompanyName != "Test")
                    .Where(x => x.FullCompanyName != "")
                    .Select(x => x.Address).ToArray())
        {
            user.CompanyAddress = w;
            var location = new GoogleLocationService();
            var point = location.GetLatLongFromAddress(w);

            user.Latitude = point.Latitude;
            user.Longtitude = point.Longitude;
        }

            return View(user);
    }

在我的(模特)上:

    public class VerifyUserModel 
    {
    public string UserName { get; set; }

    public string AccountLevel { get; set; }

    public double Latitude { get; set; }

    public double Longtitude { get; set; }

    public string CompanyAddress { get; set; }
}

然后是我的(.CSHTML):

 @foreach (var place in Model)
    { 
        <text>
        var latlng = new google.maps.LatLng(@Model.Latitude, @Model.Longtitude);
        var marker = new google.maps.Marker({
            position: latlng,
            map: map,
            title: 'Latitude: ' + latlng.Ya + ' Longitude :' + latlng.Za,
            draggable: true,
            icon: image
        });
        infoWindow = new google.maps.InfoWindow({
            content:"-- Test Address --"
        });
        infoWindow.open(map, marker);
        geocoder = new google.maps.Geocoder();
        //Update postal address when the marker is dragged
        google.maps.event.addListener(marker, 'click', function () { //dragend
            geocoder.geocode({ latLng: marker.getPosition() }, function (responses) {
                if (responses && responses.length > 0) {
                    infoWindow.setContent(
                    "<div style=\"font-size:smaller;\">" + responses[0].formatted_address
                    + "<br />"
                    + "Latitude: " + marker.getPosition().lat() + "&nbsp"
                    + "Longitude: " + marker.getPosition().lng() + "</div>"
                    );
                    infoWindow.open(map, marker);
                } else {
                    alert('Error: Google Maps could not determine the address of this location.');
                }
            });
            map.panTo(marker.getPosition());
        });
        // Close the marker window when being dragged
        google.maps.event.addListener(marker, 'dragstart', function () {
            infoWindow.close(map, marker);
        });
        </text>
    }

0 个答案:

没有答案