google-maps-api-3 - 如何让我的 Google 地图显示在 .NetCore Web 应用程序中?
问题描述
我正在遵循https://developers.google.com/maps/documentation/javascript/importing_data中的教程。当我运行我的应用程序时,我可以在我的开发工具中看到我从服务器收到 200 响应,但我的地图没有显示。我没有控制台错误。
我从谷歌云获得了我的 API 密钥。我正在使用 Maps JavaScript API。我的帐单已启用。这是一个干净的 .Net 核心 Web 应用程序。
索引.html
<div id="map"></div>
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 2,
center: new google.maps.LatLng(2.8,-187.3),
mapTypeId: 'terrain'
});
// Create a <script> tag and set the USGS URL as the source.
var script = document.createElement('script');
// This example uses a local copy of the GeoJSON stored at
// http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojsonp
script.src = 'https://developers.google.com/maps/documentation/javascript/examples/json/earthquake_GeoJSONP.js';
document.getElementsByTagName('head')[0].appendChild(script);
}
// Loop through the results array and place a marker for each
// set of coordinates.
window.eqfeed_callback = function(results) {
for (var i = 0; i < results.features.length; i++) {
var coords = results.features[i].geometry.coordinates;
var latLng = new google.maps.LatLng(coords[1],coords[0]);
var marker = new google.maps.Marker({
position: latLng,
map: map
});
}
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY&callback=initMap">
</script>
网站.css
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
解决方案
我通过创建新的控制器操作和视图解决了这个问题。然后,我将 JavaScript 和 HTML 代码添加到新创建的视图中,并从浏览器导航到该控制器操作,并按预期显示谷歌地图。
推荐阅读
- flutter - 如何在颤动中将行和列的大小设置为其父小部件?
- c# - 每次迭代或循环浏览文档列表时,如何创建新的 PDF 文件?
- c - 与位置无关的可执行文件:什么是“主要可执行二进制文件”?
- jquery - 在 php 字符串中使用替换函数
- com - 如何让我的 WiX 产品脚本正确安装我的活动 x 控件?
- python - 一种用组有效地替换数组元素的算法
- angular - Nginx 重新加载 Angular 应用程序
- html - 如果空单元格更改行颜色,则html css表
- azure-devops - 如何使发布管道期间生成的文件可访问?
- python - 自动决定从python中的相关矩阵中删除哪个特征