简体   繁体   English

Google Maps使用AJAX添加标记

[英]Google Maps Adding Markers with AJAX

SO I've read a bunch about Closure Issues, and I'm assuming that's what this is, but I'm not sure how to fix it. 因此,我已经阅读了很多有关关闭问题的信息,我假设这就是问题所在,但是我不确定如何解决它。

The Problem: Basically, I only get 1 Marker, Which I'm assuming is because I'm using the same "marker" variable? 问题:基本上,我只得到1个Marker,我假设这是因为我使用的是相同的“ marker”变量? But I'm not sure, nor do I see an easy solution to fix it. 但是我不确定,也看不出解决此问题的简单解决方案。 I'm sure there's something obvious and I'd love a hand from someone who actually knows Javascript, as opposed to me who just screws with it until I get the result I need. 我敢肯定有一些明显的东西,我会喜欢真正了解Javascript的人的帮助,而不是我一直把它拧紧直到我得到所需结果的人。

Thanks! 谢谢!

<script>
var geocoder;
var map;
var markersArray = [];
var plocation = [];

function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(39.50, -98.35);
var myOptions = {
    zoom: 4,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}

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

function codeAddress() {
    //Delete Existing Markers
    clearOverlays();
    deleteOverlays();
    //Geocode and Add the New One + Results.
    var address = document.getElementById("address").value;
    geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        //add the center marker
        var patientslocation =results[0].geometry.location;
        addMarker(patientslocation, "Patient");
        //Zoom in on the Region.
        map.setZoom(10);
        //Call to Our API
        $.getJSON("map/search", { provider_type: "01", loc: '"'+patientslocation+'"' },function(data) {
            //Parse Results
            var htmlstring = "";
            var arraylength = data.length-1;
            console.log("Result Count (base 0): "+arraylength)
            $(data).each(function(i,val){

                //Build HTML String for Side Bar
                if (val.name){
                    htmlstring = htmlstring + "<h3>"+val.name+"</h3>";
                }
                if (val.address){
                    htmlstring = htmlstring + val.address +"<br/>";
                }
                if (val.phone){
                    htmlstring = htmlstring +val.phone +"<br/>";
                }
                //Build HTML Strings for Each Marker Hover

                //Load the Array of Markers
                plocation[i] = new google.maps.LatLng(val.lat, val.lng);
                console.log(i +" : " +plocation[i]);

                if(i === arraylength){
                    console.log("Load plocations called")
                    load_plocations();
                }
            });
            $('#prov_list').html(htmlstring);

        });

} else {
    alert("Geocode was not successful for the following reason: " + status);
}
});
}

function load_plocations(){
    $(plocation).each(function(k,v){
        console.log("Calling AddMarker: "+v)
        addMarker(v,k);
    });
}

function addMarker(location, name) {
   console.log("Adding Marker: "+location)
    marker = new google.maps.Marker({
        position: location,
        map: map
    });
    markersArray.push(marker);
}
//Clears the Markers from the map.
function clearOverlays() {
    console.log("Clearing Overlays");
    if (markersArray) {
        for (i in markersArray) {
            markersArray[i].setMap(null);
        }
    }
}

// Deletes all markers in the array by removing references to them
function deleteOverlays() {
    console.log("Deleting Overlays");
    if (markersArray) {
        for (i in markersArray) {
            markersArray[i].setMap(null);
        }
        markersArray.length = 0;
    }
}

Try declaring marker using var : 尝试使用var声明标记

var marker = new google.maps.Marker({

That will make it a locally-scoped object, thus creating a new one for each iteration. 这将使其成为本地范围的对象,从而为每次迭代创建一个新对象。

Without the var , it becomes global (ie, scoped to the window object). 没有var ,它将变为全局(即,作用域为window对象)。 I'm not 100% sure but I think this may, as you wrote, overwrite the same marker on each iteration, rather than creating a new one. 我不确定100%,但是我认为,正如您所写的那样,每次迭代可能会覆盖相同的标记,而不是创建一个新的标记。

The geocoder is subject to rate limiting and quotas. 地理编码器受速率限制和配额限制。 It has never been a good idea to geocode multiple points "on the fly" to display a web page, which is what you are attempting to do in your loop, that will only work for small numbers of markers. 即时对多个点进行地理编码以显示网页是一个好主意,这是您要在循环中尝试做的事情,仅适用于少量标记。 Here is an example (using the v2 API) that geocodes a list of addresses from xml transferred via AJAX. 这是一个示例(使用v2 API) ,该示例对通过AJAX传输的xml中的地址列表进行了地理编码。

Here is an article that is part of the google maps API v3 documentation that discusses geocoding strategies. 这是google maps API v3文档的一部分,该文章讨论了地理编码策略。

The best solution is if the points are known ahead of time, geocode them offline and store the coordinates for use when your page loads. 最好的解决方案是,如果提前知道这些点,请对其进行离线地理编码,并存储页面加载时使用的坐标。 If they are entered by the user, use the client side geocoder built into the API. 如果用户输入了它们,请使用API​​内置的客户端地理编码器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM