Tomothy
Tomothy

Reputation: 11

Google Maps API - Geocode & Search Nearby

I'm trying to merge together a GOOGLE geocoder script and a 'search nearby' script. I cannot seem to merge it successfully. Help?

The geocoder script lies here:

var geocoder;
var map;
function initialize() {
  geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(-34.397, 150.644);
  var mapOptions = {
    zoom: 8,
    center: latlng
  }
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}

function codeAddress() {
  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);
      var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
      });
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}

google.maps.event.addDomListener(window, 'load', initialize);

And the search nearby script lies here:

var map;
var infowindow;

function initialize() {
  var pyrmont = new google.maps.LatLng(-33.8665433, 151.1956316);

  map = new google.maps.Map(document.getElementById('map-canvas'), {
    center: pyrmont,
    zoom: 15
  });

  var request = {
    location: pyrmont,
    radius: 500,
    types: ['store']
  };
  infowindow = new google.maps.InfoWindow();
  var service = new google.maps.places.PlacesService(map);
  service.nearbySearch(request, callback);
}

function callback(results, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
      createMarker(results[i]);
    }
  }
}

function createMarker(place) {
  var placeLoc = place.geometry.location;
  var marker = new google.maps.Marker({
    map: map,
    position: place.geometry.location
  });

  google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(place.name);
    infowindow.open(map, this);
  });
}

google.maps.event.addDomListener(window, 'load', initialize);

So I'm posting on here because I have no idea how to merge them. I tried to merge the scripts together but I couldn't get it to work. Any ideas?

Thanks.

Upvotes: 1

Views: 2116

Answers (1)

hwsw
hwsw

Reputation: 2606

Could be something like this sketch below. Not sexy, but works.

Don't forget to include the places libary.

JS

var geocoder;
var map;
var infowindow;
var pyrmont = new google.maps.LatLng(-33.8665433, 151.1956316);

function initialize() {  
  var latlng = new google.maps.LatLng(-34.397, 150.644);
  var mapOptions = {
    zoom: 8,
    center: latlng
  }

 map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

 codeAddress();

  var request = {
    location: pyrmont,
    radius: 500,
    types: ['store']
  };

  infowindow = new google.maps.InfoWindow();
  var service = new google.maps.places.PlacesService(map);
  service.nearbySearch(request, callback);
}


function codeAddress() {
  geocoder = new google.maps.Geocoder();  
  var address = '100 Murray St, Pyrmont NSW, Australia';   
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
      });
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}

function callback(results, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
      createMarker(results[i]);
    }
  }
}

function createMarker(place) {
  var placeLoc = place.geometry.location;
  var marker = new google.maps.Marker({
    map: map,
    position: place.geometry.location
  });

  google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(place.name);
    infowindow.open(map, this);
  });
}

google.maps.event.addDomListener(window, 'load', initialize);

http://jsfiddle.net/iambnz/c9ovns0o/

EDIT:

Or you can simply copy and paste this html file, this should work.

http://lab.sourcloud.com/stackoverflow/26071099/

EDIT 2:

First geocode the address and then show places nearby.

var geocoder;
var map;
var infowindow;
//var pyrmont = new google.maps.LatLng(-33.8665433, 151.1956316);
var nearbysearchloc = '';

function initialize() {  
  var latlng = new google.maps.LatLng(-34.397, 150.644);
  var mapOptions = {
    zoom: 8,
    center: latlng
  }
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
codeAddress();
}

function codeAddress() {
  geocoder = new google.maps.Geocoder();  
  var address = '100 Murray St, Pyrmont NSW, Australia';   
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);        
      var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
      });
    ParseLocation(results[0].geometry.location);
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}

function callback(results, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
      createMarker(results[i]);
    }
  }
}

function createMarker(place) {
  var placeLoc = place.geometry.location;
  var marker = new google.maps.Marker({
    map: map,
    position: place.geometry.location
  });

  google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(place.name);
    infowindow.open(map, this);
  });
}

function ParseLocation(location) {
      var lat = location.lat().toString().substr(0, 12);
      var lng = location.lng().toString().substr(0, 12);

nearbysearchloc = new google.maps.LatLng(lat, lng);
ShowPlacesAroundMe(nearbysearchloc);
}

function ShowPlacesAroundMe(location){
var request = {
    location: location,
    radius: 500,
    types: ['store']
  };
  infowindow = new google.maps.InfoWindow();
  var service = new google.maps.places.PlacesService(map);
  service.nearbySearch(request, callback);    

}

google.maps.event.addDomListener(window, 'load', initialize);

http://jsfiddle.net/iambnz/c0omhyep/

Upvotes: 1

Related Questions