正在使用文本框中的地址从geoloc设置标记

Setting marker from geoloc with address from textbox

本文关键字:地址 geoloc 设置 文本      更新时间:2023-09-26

我有这部分代码。我想放一个标记,当文本框中的文本发生变化时——它与函数changeValue()一起工作——获取新地址。

然后,我想把这个标记,当地址变量将改变。怎么办?

我尝试使用下面的代码,但出现了以下错误:Error: ReferenceError: changeValue is not defined怎么了?

     (...)
    <asp:TextBox Style="width: 300px;" runat="server" ID="tbStreet" onchange="changeValue()"></asp:TextBox>
     (...) // more 5 textboxes
   <script type='text/javascript'>
          (...) // 2 long string arrays.
        var address;
        function mapaStart() {
            var wspolrzedne = new google.maps.LatLng(52.22105994970536, 19.22609921875007);
            var opcjeMapy = {
                zoom: 5,
                center: wspolrzedne,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("mapka"), opcjeMapy);

        function changeValue() {
            var ulica;
            var miasto;
            var woj1;
            var panstwo1;
            ulica = document.getElementById("<%=tbStreet.ClientID%>").value;
            miasto = document.getElementById("<%=tbCity.ClientID%>").value;
            woj1 = woj[document.getElementById("<%=ddlProvince.ClientID%>").value];
            panstwo1 = panstwa[document.getElementById("<%=ddlCountry.ClientID%>").value];
            address = panstwo1 + ", " + woj1 + ", " + miasto + ", " + ulica;
            alert(address);
        }   
            var geokoder = new google.maps.Geocoder();
            map.setCenter(address.geometry.location);
            var marker = new google.maps.Marker(
                {
                    map: map,
                    position: address.geometry.location,
                }
            );
        } 
    </script>

您的changeValue函数是mapaStart函数的本地函数。在HTML click/onchange侦听器运行的全局上下文中无法访问它。若要使它全局化,请将它移到mapaStart函数之外。