Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 谷歌地图-禁用元素,如果谷歌无法确定地址位置_Javascript_Jquery_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript 谷歌地图-禁用元素,如果谷歌无法确定地址位置

Javascript 谷歌地图-禁用元素,如果谷歌无法确定地址位置,javascript,jquery,google-maps,google-maps-api-3,Javascript,Jquery,Google Maps,Google Maps Api 3,我正在使用以下脚本将待售房屋加载到我的谷歌地图上。我想知道这是否可行,是否可行——我需要一位非常优秀的谷歌地图程序员的帮助来添加它 我在寻找什么 如果左侧面板中的一个地址无法被Google找到或超出了边界,它仍然会将元素和地址加载到左侧面板中——但通过向该元素添加类,该元素处于禁用状态。这能做到吗 映射边界 这是边界,如果标记在该区域内,它将添加标记,否则将禁用元素。如果无法完成带边界的搜索,我将选择Google不查找它。 我正在使用的脚本 以下是我到目前为止所掌握的内容 我根据上面的站点编

我正在使用以下脚本将待售房屋加载到我的谷歌地图上。我想知道这是否可行,是否可行——我需要一位非常优秀的谷歌地图程序员的帮助来添加它

我在寻找什么
如果左侧面板中的一个地址无法被Google找到或超出了边界,它仍然会将元素和地址加载到左侧面板中——但通过向该元素添加类,该元素处于禁用状态。这能做到吗

映射边界
这是边界,如果标记在该区域内,它将添加标记,否则将禁用元素。如果无法完成带边界的搜索,我将选择Google不查找它。

我正在使用的脚本

以下是我到目前为止所掌握的内容
我根据上面的站点编写了以下脚本,只是想将缺少的location元素添加到这个概念中

var map;
var markers = [];
var lastinfowindow;
var locIndex;
if (!Array.prototype.forEach) {
    Array.prototype.forEach = function(fn, scope) {
        for (var i = 0, len = this.length; i < len; ++i) {
            fn.call(scope, this[i], i, this);
        }
    }
}
var data = [
    {address:'123 GODERICH ST GODERICH ONTARIO',title:'123 GODERICH ST'},
    {address:'123 KNOWLES LANE KINCARDINE ONTARIO',title:'123 KNOWLES LANE'}
];
function Initialize() {
    var latlng = new google.maps.LatLng(44.00, -80.00);
    var mapOptions = {
        zoom : 8,
        center : latlng,
        mapTypeId : google.maps.MapTypeId.ROADMAP,
        scaleControl : true,
        mapTypeControl : false,
        navigationControlOptions : {
            style : google.maps.NavigationControlStyle.SMALL
        },
        scrollwheel : false,
        minZoom : 7,
        maxZoom : 15,
        keyboardShortcuts : false,
        disableDoubleClickZoom : true,
        draggable : true,
        backgroundColor : '#FFFFFF'
    };
    var mapStyles = [{
        featureType: 'poi',
        stylers: [{
            visibility: 'off'
        }]
    }];
    var styledMap = new google.maps.StyledMapType(mapStyles, {name: 'Styled Map'});
    map = new google.maps.Map(document.getElementById('map'), mapOptions);
    geocoder = new google.maps.Geocoder();
    map.mapTypes.set('map_style', styledMap);
    map.setMapTypeId('map_style');
    icon = new google.maps.MarkerImage('/pointer.png', new google.maps.Size(19, 29), new google.maps.Point(0, 0), new google.maps.Point(8, 29));
    data.forEach(function(mapData,idx) {
        geocoder.geocode({'address':mapData.address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var marker = new google.maps.Marker({
                    map: map, 
                    position: results[0].geometry.location,
                    title: mapData.title,
                    icon: icon
                });
                var contentHtml = "<div style='width:300px;height:200px'><h3>"+mapData.title+"</h3>"+mapData.address+"</div>";
                var infowindow = new google.maps.InfoWindow({
                    content: contentHtml
                });
                google.maps.event.addListener(marker, 'click', function() {
                    infowindow.open(map,marker);
                });
                marker.locid = idx+1;
                marker.infowindow = infowindow;
                markers[markers.length] = marker;
                var sideHtml = '<p class="loc" data-locid="'+marker.locid+'"><b>'+mapData.title+'</b><br/>';
                     sideHtml += mapData.address + '</p>';
                     $('#locations').append(sideHtml); 
            }
        });
    });
}
window.onload = Initialize;
var映射;
var标记=[];
var lastinfowindow;
var指数;
if(!Array.prototype.forEach){
Array.prototype.forEach=函数(fn,范围){
对于(变量i=0,len=this.length;i“+mapData.title+'
”; sideHtml+=mapData.address+'

'; $(“#位置”).append(sideHtml); } }); }); } window.onload=初始化;

我目前正在考虑是否能做到这一点,并将在这一过程中不断更新。谢谢

您需要检查geocode调用的状态是否为

ZERO_RESULTS
INVALID_REQUEST
或者Maps API中的另一个,例如

geocoder.geocode({'address':mapData.address}, function(results, status) {
    if (status === google.maps.GeocoderStatus.OK) {
       //handle valid address search
    } else if (status === google.maps.GeocoderStatus.INVALID_REQUEST 
                  || status === google.maps.GeocoderStatus.ZERO_RESULTS) {
       //handle no results returned
    }
}

如果您的地理编码请求返回这些状态之一,则可以将元素添加到地图中,但可以将标记设置为降低可见性状态或其他您喜欢的选项。对于类似的内容,请查看上面链接中的标记选项和相关标记文档。

Beauty,我将尝试一下Jason,看看会发生什么。