首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >谷歌地图地理编码- offsetWidth

谷歌地图地理编码- offsetWidth
EN

Stack Overflow用户
提问于 2015-01-15 15:46:11
回答 1查看 1K关注 0票数 0

我在使用google显示邮政编码时遇到了问题。但是,它给出了一个错误:

行中未处理的异常.列..。在https://maps.gstatic.com/maps-api-v3/api/js/19/6/main.js

0x800a138f - JavaScript运行时错误:无法获得未定义或空引用的属性“offsetWidth”

你能给我一些指导吗?

代码文件:

代码语言:javascript
复制
    public string location = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        using (OleDbConnection conn = new OleDbConnection())
        {
            String connection = System.Configuration.ConfigurationManager.ConnectionStrings["google_string"].ConnectionString;

            conn.ConnectionString = connection;
            conn.Open();

            String sql = "select * from tbl_Post";
            OleDbCommand cmd = new OleDbCommand(sql,conn);
            var rdr = cmd.ExecuteReader();
            rdr.Read();

            if(!string.IsNullOrEmpty(rdr["location"].ToString()))
            {

                location = rdr["location"].ToString();
            }

        }
    }
}

}

asp文件:

代码语言:javascript
复制
    <div>
        <%--map canvas--%>
        <style type="text/css">
            html, body, #map-canvas {
                height: 100%;
                margin: 0;
                padding: 0;
            }
        </style>


        <script type="text/javascript"
            src="https://maps.googleapis.com/maps/api/js?key=....">

        </script>

        <script type="text/javascript">
            var geocoder;
            var map;

            function initialize() {
                geocoder = new google.maps.Geocoder();

                var latlng; 
                var mapOptions = {
                    //street level

                    center: new google.maps.LatLng(-34.397, 150.644),
                    zoom: 18,

                }


                map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

                //load post code address (CALLING FUNCTION)
                codeAddress();
            }


            google.maps.event.addDomListener(window, "load", initialize);

            function codeAddress() {


                geocoder.geocode({ 'address': '<%= location%>' }, function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        map.setCenter(results[0].geometry.location);

                        var marker = new google.maps.Marker({
                            map: map,
                            position: results[0].geometry.location
                        });
                    } else {
                        alert("Geocode was not succesfull for the following reason" + status);
                    }
                });  
            }
        </script>

    </div>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-15 16:10:53

这个问题通常是由于在需要访问映射divjavascript运行之前没有呈现它。

您就快到了,就像您没有id="map-canvas"元素一样。因此,给您的div一个id,然后它应该可以正常工作。

代码语言:javascript
复制
<div id="map-canvas"></div>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27967322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档