2014-09-30 35 views
0

我正在使用以下腳本將待售房屋加載到我的Google地圖上。我想知道這是否可行,如果是 - 我需要一位非常好的Google Map程序員的幫助來幫助添加它。Google地圖 - 如果Google無法確定地址位置,請停用元素

什麼我看對於
如果左側面板中的一個地址不能被谷歌或發現超出界限的,它仍然將元素加載和地址在左側面板中 - 但已禁用通過向該元素添加一個類來添加狀態。這可以做到嗎?

映射邊界
這裏是界限,如果標記是這個領域它添加標記其他禁用元素中。如果無法實現邊界,我會選擇Google找不到它。
http://www.freemaptools.com/radius-around-point.htm?clat=44.512176171071054&clng=-81.08184814453125&r=96.39586143951128&lc=FFFFFF&lw=1&fc=00FF00

腳本我使用
http://www.raymondcamden.com/demos/2012/dec/1/new4.html

這裏是我有什麼到目前爲止
我放在一起後續腳本基於上面的網站上,只是希望添加缺少的位置元素這個概念。

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; 

我目前正在看看我是否可以做到這一點,並會一路更新。謝謝!!

回答

0

您需要檢查,如果地址解析呼叫的狀態或者是

ZERO_RESULTS 
INVALID_REQUEST 

或地圖API中的其他status codes之一,如

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 
    } 
} 

如果你有一個地址解析請求返回其中一個狀態,您可以將該元素添加到地圖,但將標記設置爲縮小的可見性狀態或您喜歡的其他選項。對於這種情況,請查看上面鏈接中的MarkerOptions和相關的Marker文檔。

+0

美麗,我會試試這個賈森,看看會發生什麼。 – Tim 2014-10-01 23:25:51