2012-03-25 3 views
3

Google Maps Store Locatorチュートリアルhttps://developers.google.com/maps/articles/phpsqlsearch_v3に従って、ユーザーの所在地と半径を取得してすべての店舗を出力するロケータを作成しましたユーザーの場所のその半径内の場所。そして、ほとんどの場合、それは動作します!Google Maps API v3 Store Locator with Radius:ユーザーの半径に店舗がない場合の地図出力を制御する

私の問題は、場所と半径を入力し、その半径内に店舗がない場合、私のロケータは地図を太平洋の中心に集中させるということです。

私は他の店舗ロケータの質問を調べるために最善を尽くしましたが、すべてロケータ自体が機能するようになっていました。私が逃した何かがあるならば、私は謝罪します!また、私はjavascriptについては初心者ですので、コードの背後にあるロジックの回答は高く評価されています!

ありがとうございます!


function searchLocationsNear(center) { 
clearLocations(); 

var radius = document.getElementById('radiusSelect').value; 
var searchUrl = 'storelocator_getXML.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius; 
downloadUrl(searchUrl, function(data) { 
    var xml = parseXml(data); 
    var markerNodes = xml.documentElement.getElementsByTagName("marker"); 
    var bounds = new google.maps.LatLngBounds(); 
    for (var i = 0; i < markerNodes.length; i++) { 
    var name = markerNodes[i].getAttribute("name"); 
    var address = markerNodes[i].getAttribute("address"); 
    var phone = markerNodes[i].getAttribute("phone"); 
    var fax = markerNodes[i].getAttribute("fax"); 
    var directions = markerNodes[i].getAttribute("directions"); 
    var distance = parseFloat(markerNodes[i].getAttribute("distance")); 
    var latlng = new google.maps.LatLng(
     parseFloat(markerNodes[i].getAttribute("lat")), 
     parseFloat(markerNodes[i].getAttribute("lng"))); 

    createOption(name, address, distance, i); 
    createMarker(latlng, name, address, phone, fax, directions); 
    bounds.extend(latlng); 
    } 
    map.fitBounds(bounds); 
    locationSelect.style.visibility = "visible"; 
    locationSelect.onchange = function() { 
    var markerNum = locationSelect.options[locationSelect.selectedIndex].value; 
    google.maps.event.trigger(markers[markerNum], 'click'); 
    }; 
    }); 
} 

答えて

6

試行錯誤の多くの後、私はアハモーメントを持っていた、と私は太平洋のうち、私のマップを維持するためのif/else文を配置する場所を見つけた:

function searchLocationsNear(center) { 
clearLocations(); 

var radius = document.getElementById('radiusSelect').value; 
var searchUrl = 'storelocator_getXML.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius; 
downloadUrl(searchUrl, function(data) { 
    var xml = parseXml(data); 
    var markerNodes = xml.documentElement.getElementsByTagName("marker"); 

if文をここに入れます。通常通り継続するマーカーが存在する場合、それは、右の選択半径内のすべてのマーカーの位置をつかん後だと言う:

if(markerNodes.length>0){ 

    var bounds = new google.maps.LatLngBounds(); 
    for (var i = 0; i < markerNodes.length; i++) { 
    var name = markerNodes[i].getAttribute("name"); 
    var address = markerNodes[i].getAttribute("address"); 
var phone = markerNodes[i].getAttribute("phone"); 
var fax = markerNodes[i].getAttribute("fax"); 
var directions = markerNodes[i].getAttribute("directions"); 
    var distance = parseFloat(markerNodes[i].getAttribute("distance")); 
    var latlng = new google.maps.LatLng(
      parseFloat(markerNodes[i].getAttribute("lat")), 
      parseFloat(markerNodes[i].getAttribute("lng"))); 

    createOption(name, address, distance, i); 
    createMarker(latlng, name, address, phone, fax, directions); 
    bounds.extend(latlng); 
    } 
    map.fitBounds(bounds); 

そしてここで、私は何のマーカーが存在しないとき内に何をすべきかのために、他を置きます半径:

} else { 
    alert('Sorry, there are no stores that close to your location. Try expanding your search radius.'); 
    } 

    locationSelect.style.visibility = "visible"; 
    locationSelect.onchange = function() { 
    var markerNum = locationSelect.options[locationSelect.selectedIndex].value; 
    google.maps.event.trigger(markers[markerNum], 'click'); 
    }; 

}); 
} 
+1

ありがとうございました。 – jerrygarciuh

関連する問題