谷歌地图javascript代码隐藏视觉工作室

时间:2015-06-29 15:45:00

标签: 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" />

我做了一些改动,现在它正在发挥作用。

2 个答案:

答案 0 :(得分:0)

我认为您应该在标记中使用myMap而不是map-canvas,例如:

 @"map: myMap });";

答案 1 :(得分:0)

首先,在你的代码中

geocoder.geocode( { 'address': " + txtLocal.Text + "}," +
                             @"map: map_canvas });";

@“map:map_canavas应该说:

@"map: myMap

或者你也可以尝试将它添加到你的代码中:

<script type="text/javascript"
  src="https://maps.googleapis.com/maps/api/js?key=>>>>>YOUR_API_KEY<<<<<<">
</script>
</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 type="text/javascript"> 
    $(function () {
        initialize();
    });
</script>

要获得Api Key,请尝试: https://developers.google.com/maps/documentation/javascript/tutorial

相关问题