javascript - 谷歌地图javascript代码隐藏 Visual Studio

标签 javascript c# asp.net google-maps google-maps-api-3

我在 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/

相关文章:

asp.net - 将不同的用户重定向到另一个 URL

asp.net - 从 localhost 在 ASP.NET 中发送电子邮件

javascript - 如何使用 Javascript 简单地循环遍历数组?

javascript - URL - 编码 (JavaScript) 方法 ="GET"

javascript - 在匿名范围内使用 Javascript Prototype 对象

C# 将 SQL Server 消息输出到文本文件

c# - 将 PDF 流式传输到 iframe

javascript - Qtranslate 每种语言的自定义字体问题

c# - 我在哪里可以找到关于冒泡的好教程?

c# - 如何使用 vs 2010、c# 以编程方式在 Word 2007 文档中插入或编辑饼图?