最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Maps API v3 Store Locator with Radius: controlling map output when there are no stores in the user's

programmeradmin0浏览0评论

I followed the Google Maps Store Locator Tutorial and built a locator that takes a user's location and a radius, then outputs all store locations within that radius of the user's location. And for the most part, it works!

My problem is that when I input a location and radius and there are no stores within that radius, my locator centers the map to the middle of the Pacific Ocean.

I did my best to research other store locator questions, but they all were about getting the locator itself to work. If there's something I missed, I apologize! Also, I'm a beginner when it es to javascript, so answers with the logic behind code are appreciated!

Thanks!


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');
   };
  });
}

I followed the Google Maps Store Locator Tutorial https://developers.google./maps/articles/phpsqlsearch_v3 and built a locator that takes a user's location and a radius, then outputs all store locations within that radius of the user's location. And for the most part, it works!

My problem is that when I input a location and radius and there are no stores within that radius, my locator centers the map to the middle of the Pacific Ocean.

I did my best to research other store locator questions, but they all were about getting the locator itself to work. If there's something I missed, I apologize! Also, I'm a beginner when it es to javascript, so answers with the logic behind code are appreciated!

Thanks!


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');
   };
  });
}
Share Improve this question edited Mar 27, 2012 at 2:53 stockj asked Mar 25, 2012 at 16:32 stockjstockj 911 silver badge6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

After a lot of trial and error, I had an aha moment, and I found out where to put an if/else statement to keep my map out of the Pacific Ocean:

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");

I put an if statement here. It's right after grabbing all the marker locations within the selected radius and says, if there are markers to continue as normal:

   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);

And here, I put the else, for what to do when there are no markers within the radius:

  } 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');
   };

 });
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论