我在 C# 文件代码隐藏中有这个 javascript 代码:
string markers = @"var address = '" + txtLocal.Text + @"';
geocoder.geocode( { 'address': address}, function(results, status) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
title: address,
position: results[0].geometry.location
});
});" ;
Literal1.Text = @"
<script type='text/javascript'>
function initialize() {
geocoder = new google.maps.Geocoder();
var mapOptions = { zoom: 15, mapTypeId: google.maps.MapTypeId.HYBRID };
var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);"
+ markers + @"
}
</script>";
我在 .aspx 文件中有这段代码:
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBF-_RUuJxittvOu3E0SwutoD8LU3BZkPI"></script>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"type="text/javascript"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=geometry,visualization&sensor=false"></script>
<script type="text/javascript">
$(function () {
initialize();
});
</script>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
<div id="map_canvas" style="width: 500px; height: 300px" />
我做了一些更改,现在它可以正常工作了。
最佳答案
我认为你应该在标记中使用 myMap
而不是 map-canvas
例如:
@"map: myMap });";
关于javascript - 谷歌地图javascript代码隐藏 Visual Studio ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31120019/