Google maps api 3 自动完成生成多个标记

Google maps api 3 自动完成生成多个标记,google-maps-api-3,Google Maps Api 3,我的目标是有几个具有自动完成功能的输入字段,每个输入将在地图上生成一个标记。然后我需要放大地图以包含所有标记。我有这部分工作,没有自动完成功能。我曾尝试将GoogleMapsAPI将自动完成代码添加到我的for循环中,但没有效果。它为所有输入字段提供自动完成功能,但仅为最后一个输入放置一个标记 欢迎提供任何帮助/见解/指导 这是我的代码,自动完成当前被删除,因为我无法让它工作 <div class="container-fluid"> <div class="row

我的目标是有几个具有自动完成功能的输入字段,每个输入将在地图上生成一个标记。然后我需要放大地图以包含所有标记。我有这部分工作,没有自动完成功能。我曾尝试将GoogleMapsAPI将自动完成代码添加到我的for循环中,但没有效果。它为所有输入字段提供自动完成功能,但仅为最后一个输入放置一个标记

欢迎提供任何帮助/见解/指导

这是我的代码,自动完成当前被删除,因为我无法让它工作

<div class="container-fluid">
      <div class="row-fluid">
        <div class="span4 well">
            <div id="locations">
                <form>
                    <label>Location 0</label>
                    <input id="address0" type="text" size="50" >
                    <button id="clearField0">Clear</button>

                <div class="panel">
                    <label>Location 1</label>
                    <input id="address1" type="text" size="50">
                    <button id="clearField0">Clear</button>
                </div>
                <div class="panel">
                    <label>Location 2</label>
                    <input id="address2" type="text" size="50">
                    <button id="clearField0">Clear</button>
                </div>
                </form>
            </div>
            <button id="addField">+</button>
            <button id="deleteField">-</button>
            <button id="submit">Submit form</button>
        </div>
        <div class="span8">
            <div id="map-wrapper">
                <div id="google-map"></div>
            </div>
        </div>
      </div>
    </div>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCtNo_o0u8wYeqgiFF-KpvAtEy18T-PvAo&sensor=false&callback=createMap"></script>

    <script type="text/javascript">

        var inputFields = parseInt($('form input').size());
        var markers = [];


        var createMap = function() {
            var latlngbounds = new google.maps.LatLngBounds();
            var geocoder = new google.maps.Geocoder();

            var myOptions = {
                center : new google.maps.LatLng(35.9081, -78.8628), //center to RTP
                zoom   : 12,
                zoomControl: true,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            },

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

            $("#submit").on("click", function() {
                setAllMap(null);
                markers.pop();
                for (i=0; i<inputFields; i++) {
                    var location = $("#address" + i).val();
                    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
                            });
                            latlngbounds.extend(results[0].geometry.location);
                            map.fitBounds(latlngbounds);
                        } else {
                            alert("Geocode was not successful for the following reason: " + status);
                        }
                    });
                };
            });

            function setAllMap(map) {
                for (var i = 0; i < markers.length; i++) {
                    markers[i].setMap(map);
                }
            }

            $('#addField').click(function(){        //add input field to bottom of form
                $('form').append('<div class="panel"><label>Location '+ inputFields +'</label><input id="address'+ inputFields +'" type="text" size="50"><button id="clearField'+ inputFields +'">Clear</button></div>');
                inputFields++;
            });

            $('#deleteField').click(function(){     //delete last input field
                $('.panel').last().remove();
                inputFields--;
            });

        };



      </script>

位置0
清楚的
地点1
清楚的
地点2
清楚的
+
-
提交表格
var inputFields=parseInt($('form input').size());
var标记=[];
var createMap=function(){
var latlngbounds=new google.maps.latlngbounds();
var geocoder=new google.maps.geocoder();
变量myOptions={
中心:新的google.maps.LatLng(35.9081,-78.8628),//中心到RTP
缩放:12,
动物控制:对,
mapTypeId:google.maps.mapTypeId.ROADMAP
},
map=new google.maps.map(document.getElementById(“谷歌地图”),myOptions);
$(“#提交”)。在(“单击”,函数(){
setAllMap(空);
markers.pop();

对于(i=0;i我的第一个问题是在API参考中包括places库。接下来,我只需要从Google的places Autocomplete示例页面中获得两行代码,而不是它们提供的整个脚本:

var input = /** @type {HTMLInputElement} */(document.getElementById('searchTextField'));
var autocomplete = new google.maps.places.Autocomplete(input);

我的for循环中的这两行代码解决了这个问题。再次感谢Molle博士的评论,他的评论让我重新思考了我需要什么代码。

我的第一个问题是在API参考中包含places库。接下来,我只需要从Google的places Autocomplete示例页面中获得两行代码,而不是它们提供的整个脚本:

var input = /** @type {HTMLInputElement} */(document.getElementById('searchTextField'));
var autocomplete = new google.maps.places.Autocomplete(input);

我的for循环中的这两行解决了这个问题。再次感谢Molle博士的评论,他的评论让我重新思考了我需要什么代码。

你应该发布你如何尝试创建自动完成(目前还不清楚你尝试实现哪种自动完成……google.maps.places,jquery ui?)当您尝试使用google.maps.places.Autocomplete时,您的代码当前将失败,因为您忘记加载places库,但当加载places库且自动完成已按预期应用于3个输入的meworks的your代码时(也会在没有自动完成的情况下)感谢您的评论@Dr.Molle,我能够找到答案。我包括了places库(我一定是在这里发布之前意外删除了该库),并从places Autocomplete示例页面中删除了我包含的内容:。感谢您的快速回复!您应该发布如何尝试创建Autocomplete(目前还不清楚您尝试实现哪种类型的automcomplete…google.maps.places,jquery ui?)当您尝试使用google.maps.places.Autocomplete时,您的代码当前将失败,因为您忘记加载places库,但当加载places库且自动完成已按预期应用于3个输入的meworks的your代码时(也会在没有自动完成的情况下)感谢您的评论@Dr.Molle,我能够找到答案。我包括了places库(我一定是在这里发布之前意外删除的),并从places Autocomplete示例页面中删减了我包含的内容:。感谢您的快速回复!