gpt4 book ai didi

c# - 将 Google map 包含在 .Net Core 中的正确方法?

转载 作者:太空狗 更新时间:2023-10-29 23:43:46 25 4
gpt4 key购买 nike

我有一个 .Net Core 应用程序,在我的一个 View 中,我想要一个 Google map ,我希望能够在其中绘制动态多段线。

索引.cshtml

我在我的 View 中包含了一个谷歌地图,如下所示:

<div class="col-md-6 col-lg-6">
<h3>My Google Maps Demo</h3>
<div id="map"></div>
</div>

<script>
function initMap() {
var center = { lat: 55.92965249, lng: 12.47840507 };
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: uluru
});

$.get()
$.post()

route.setMap(map);
}
</script>

在我的 _Layout.cshtml 中我有:

<script async defer
src="https://maps.googleapis.com/maps/api/js?key=...api key...&callback=initMap">
</script>

所有这些都按预期工作,但我不确定这是否是在 .Net Core 应用程序中显示 Google map 的正确方式?

此外,我希望能够在我的 Google map 实现中绘制动态路线。这是通过以下代码完成的

var route = new google.maps.Polyline({
path: routeCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});

其中 routeCoordinates 是坐标列表:

var routeCoordinates = [
{lat: 55.92965249, lng: 12.47840507},
{lat: 55.92941392, lng: 12.47832253},
{lat: 55.92918626, lng: 12.47824027},
...
{lat: 55.91474539, lng: 12.47145191},
{lat: 55.91450191, lng: 12.47139283},
{lat: 55.91425197, lng: 12.47134614}
]

以上所有内容当然是在我的 Index.cshtml 中静态完成的。

那么有没有更好的方法呢?我将如何使用我的 Controller 动态地(希望如此)添加和删除行?我希望是这样的:

public async Task<IActionResult> Index()
{
return View(await _context.Trips.ToListAsync());
//context is my db
}

现在它不存在,但我将通过我的 EF 实现从我的上下文中获取坐标列表。

编辑:

自从我发布这篇文章后,我继续了一点,现在我有一个 PartialView,我想在单击表格行时加载它:

MapPartialView.cshtml

@model IEnumerable<LngLat>

<div class="col-md-6 col-lg-6">
<h3>My Google Maps Demo</h3>
<div id="map"></div>
</div>

<script>
function initMap() {
var center = { lat: 55.92965249, lng: 12.47840507 };
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: center
});

//$.get();
//$.post();

routeCoordinates = @Model;

var route = new google.maps.Polyline({
path: routeCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});

route.setMap(map);
}
</script>

还有我的:

索引.cshtml

// table header and so forth..
<tbody>
@foreach (var item in Model)
{
<tr class="trips" data-id="@item.TripID" data-url="@Url.Action("ExpandMap", "Trip")">
<td>
@Html.DisplayFor(modelItem => item.DeviceID)
</td>
<td>
@Html.DisplayFor(modelItem => item.StartTime)
</td>
<td>
@Html.DisplayFor(modelItem => item.Duration)
</td>
<td>
@Html.DisplayFor(modelItem => item.StartLocation)
</td>
<td>
@Html.DisplayFor(modelItem => item.EndLocation)
</td>
</tr>
}
</tbody>

<div id="partialMap"></div>

在我的:

网站.js

$('.trips').each(function (i, e) {
var _this = $(this);
var element = $(e).children('td');
element.click(function () {
//console.log("Clicked! " + _this.data('url') + " " + _this.data('id'));
$("#partialMap").load(_this.data('url'), { id: _this.data('id') }, function () {
$("#partialMap").slideDown('200');
});
});

});

最后是我的 Controller 和 ExpandMap 函数:

TripController.cs

public IActionResult ExpandMap(int id)
{
var trip = _context.Trips.SingleOrDefault(t => t.TripID == id);

List<LngLat> routeCoordinates = new List<LngLat>
{
new LngLat() { lat = 55.92965249, lng = 12.47840507},
new LngLat() { lat = 55.92941392, lng = 12.47832253},
...
new LngLat() { lat = 55.91450191, lng = 12.47139283},
new LngLat() { lat = 55.91425197, lng = 12.47134614}
};

string routeCoordinatesJS = JsonConvert.SerializeObject(routeCoordinates, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

return PartialView("MapPartialView", routeCoordinatesJS);
}

但是当我运行代码时,我得到:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

最佳答案

您可以将上下文导出到经纬度列表

    public class LngLat
{
public double lat { get; set; }
public double lng { get; set; }
}


使用数据库中的数据构建列表并将其发送到 View():

public async Task<IActionResult> Index()
{
List<LngLat> routeCoordinates = await _context.Trips.Select(c=> new LngLat {lat = c.latitude, lng = c.longitude })

//Serialize your routeCoordiamte with Json.Net
string routeCoordinatesJs = JsonConvert.SerializeObject(routeCoordinates, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore })

return View(routeCoordinatesJs);
}

在你的 View() 中:

var routeCoordinates = @Model

关于c# - 将 Google map 包含在 .Net Core 中的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40787998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com