Search of addresses in a radius per kilometer

2,533 views
Skip to first unread message

SuperSals

unread,
May 17, 2011, 5:32:30 AM5/17/11
to google-map...@googlegroups.com
Hello everyone,

Start by saying that I don't speak English very well and I am a neophyte on the V3 APIGoogle Javascript  :)

Let me know how I can do to make a search of addresses within a radius per kilometer using the Google Geocoder, without having the coordinates of each point to be searched , but only the address.

Practically  I have in a MS SQL 2005 a  list of addresses of shops, and I need to know, based on a chosen shop in a radius of 30 km if there are other stores in the same area.

My research so I will see only the stores that are within 30 km from the 
chosen one .


I specify that I'm planning in vb.net, so I interogazioni to the database in vb and I have already found a way to interact with the list of addresses with the Javascript code I needto know only how to develop a function in javascript to do what I have explained in theprevious rows.


Thank you all




Rossko

unread,
May 17, 2011, 5:36:00 AM5/17/11
to Google Maps JavaScript API v3
> Practically  I have in a MS SQL 2005 a  list of addresses of shops, and I
> need to know, based on a chosen shop in a radius of 30 km if there are other
>  stores in the same area.

Don't do that in javascript at all.
See
http://code.google.com/apis/maps/articles/phpsqlsearch_v3.html
for an SQL based approach

SuperSals

unread,
May 17, 2011, 7:30:44 AM5/17/11
to Google Maps JavaScript API v3
Hello Rossko,

Thank you for your reply,

I looked at your link but the guide that I've provided does not
resolve exactly what to do

I thought, however, to calculate how many miles each store compared to
it has been selected through this function that I found:


InitDistances function () {
StartLocation var = new GLatLng (startLat, startLon);
endLocation var = new GLatLng (endLat, endLon);
var dist = StartLocation. DistanceFrom (endLocation);
/ / Convert distance to Kilometres
return (dist / 1000). toFixed (2);
}

where the latitude and longitude them income through my geocoder with
the string "results [0]. geometry.location" and if the distance is not
more than 30 km I can see that address and otherwise jump to the next
address and so on.

I only know where to put this function in my code.

This is the function that I use to display my map (function works
good).




// Set My Arrays

//This is the list of addreses
locations = new Array();

//this contain the list of description for each address
InfoString = new Array();


var map;
var geocoder;

//images
var image;
//set the zoomlevel
var zoom;

var visual;




var index = 0;

var geocoderFunction = function() {

if (index == 0) {

//var test = findObjWithClientId("hd_ind");
//elenco = test.value;
//locations = elenco.split("|");

image = "0"


locations[0] = "Piazza Maggiore, 40128, Bologna, Emilia
Romagna"
locations[1] = "Vicolo San Giobbe, 40128, Bologna, Emilia
Romagna"
locations[2] = "Via Monte Grappa, 40128, Bologna, Emilia
Romagna"
locations[3] = "Via Barberia, 40128, Bologna, Emilia
Romagna"
locations[4] = "Via Riva di reno, 40128, Bologna, Emilia
Romagna"
locations[5] = "Via Marsala, 40128, Bologna, Emilia
Romagna"
locations[6] = "Via Bocca di Lupo, 40128, Bologna, Emilia
Romagna"




InfoString[0] = "Piazza Maggiore, 40128, Bologna, Emilia
Romagna"
InfoString[1] = "Vicolo San Giobbe, 40128, Bologna, Emilia
Romagna"
InfoString[2] = "Via Monte Grappa, 40128, Bologna, Emilia
Romagna"
InfoString[3] = "Via Barberia, 40128, Bologna, Emilia
Romagna"
InfoString[4] = "Via Riva di reno, 40128, Bologna, Emilia
Romagna"
InfoString[5] = "Via Marsala, 40128, Bologna, Emilia
Romagna"
InfoString[6] = "Via Bocca di Lupo, 40128, Bologna, Emilia
Romagna"




//var test = findObjWithClientId("hd_desc");
//desc = test.value;
//InfoString = desc.split("||");

//var testimage = findObjWithClientId("hd_image");
//image = testimage.value;

//var TestzoomMap = findObjWithClientId("hd_zoom");
//zoomMap = parseInt(TestzoomMap.value);

//set the map type
var mapOpt = {
mapTypeId: google.maps.MapTypeId.ROADMAP
//center: new google.maps.LatLng(38.00, -100.00),
//zoom: 6
};

map = new
google.maps.Map(document.getElementById("map_canvas"), mapOpt);

geocoder = new google.maps.Geocoder();

}



geocoder.geocode({ 'address': locations[index] },
function(results, status) {
//document.write(status + " ; ");
if (status == google.maps.GeocoderStatus.OK) {
if (index == 0) {
map.setCenter(results[0].geometry.location);
map.setZoom(zoom);


// Cyrcle On the map
var circle = new google.maps.Circle({
center: results[0].geometry.location,
map: map,
radius: 1000 // 1 km
});

}

//Creo la mia infowindows
var infowindow = new google.maps.InfoWindow({
content: InfoString[index]
});

if (image == "0") {
var marker = new google.maps.Marker
({ map: map,
position: results[0].geometry.location,
animation: google.maps.Animation.DROP,
title: "Store"
//icon: "images/fumetto.png"
});
};
if (image == "1") {
var marker = new google.maps.Marker
({ map: map,
position: results[0].geometry.location,
animation: google.maps.Animation.DROP,
title: "Showroom"
//icon: "../../images/fumettoHQ.png"
});
};

//A Ogni ciclo assegno alla al mio Marker una
Infowindows che si aprirà al click del mouse
google.maps.event.addListener(marker, 'click',
function() {
infowindow.open(map, marker);
});


alert(results[0].geometry.location);
}



else {

alert(status + " at position" + locations[index]);


}

// Call the geocoder with a 1000ms delay
index++;
if (locations.length > index) {
setTimeout(geocoderFunction, 1000);
}
});
if (index == locations.length) {

index = 0;


}
}


Can you give me some suggestions?

Rossko

unread,
May 17, 2011, 7:52:52 AM5/17/11
to Google Maps JavaScript API v3
> Can you give me some suggestions?

Yes, don't geocode your store addresses every time the page is
viewed. It is slow, and if the geocoder makes a mistake (it's not
perfect) you cannot fix the error. I'm sure the stores don't move
around much.

Gecoding is asynchronous ; your 1000mS delay is a kludge and is not
the correct way to use asynchronous services. There are request rate
and volume limits set for geocoding, you may run into those.

You have a database of locations ; use it to store locations i.e.
store lat/longs as well. That data is under your control, you can fix
any anomalies.

Determining straight line distances then becomes trivial, even if you
must do it in javascript, but you can just as easily SELECT the right
data in SQL in the first place and so avoid sending the whole dataset
to the client for processing.

Have a look at
http://code.google.com/apis/maps/articles/geocodestrat.html

SuperSals

unread,
May 17, 2011, 12:10:51 PM5/17/11
to Google Maps JavaScript API v3
Hello Rossko,

Your tips were very helpful to me thanks
I had only a limited time and by the time I had to set aside (sadly
some of your suggestions).



"It is slow, and if the geocoder Makes A Mistake"

As for the error on the discovery of the address I fix enabling the
operator can change the address in a way that is readable by the
geocoder.


"Yes, do not store your geocode addresses Every time the page is
viewed.

The page is loaded all the stores because I seek not avnedoli already
included in the database in the form: City, Province, State / Country,
address. depending on the store interogo to search the database
according to its city and state in order to further weed out the DB.

"Gecoding is asynchronous; your 1000ms delay is a kludge and is not
the correct way to use asynchronous services. "

is very interesting when I have more time to get their hands on this
program I try to improve it as you say.

Thanks:)

Eventually I succeeded, and the function does what it should do.
I'll put you below XD



// Prepare this list from ColdFusion
locations = new Array();
InfoString = new Array();
//Ja è la latitudine del mio negozio preso in esame
var CentroLat;
//Ka è la longitudine del mio negozio preso in esame
var CentroLang;

//Ja è la latitudine del mio negozio che mi permette di ricavare
la distanza in confronto al mio centro
var StoreLat;
//Ka è la longitudine del mio negozio che mi permette di ricavare
la distanza in confronto al mio centro
var StoreLang;

var map;
var geocoder;


var image;
var zoom;
var visual;




var index = 0;

var geocoderFunction = function() {

if (index == 0) {

//var test = findObjWithClientId("hd_ind");
//elenco = test.value;
//locations = elenco.split("|");

image = "0"


locations[0] = "Piazza Maggiore, 40128, Bologna, Emilia
Romagna"
locations[1] = "Vicolo San Giobbe, 40128, Bologna, Emilia
Romagna"
locations[2] = "Via Monte Grappa, 40128, Bologna, Emilia
Romagna"
locations[3] = "Via Barberia, 40128, Bologna, Emilia
Romagna"
locations[4] = "Via Riva di reno, 40128, Bologna, Emilia
Romagna"
locations[5] = "Via Marsala, 40128, Bologna, Emilia
Romagna"
locations[6] = "Via Bocca di Lupo, 40128, Bologna, Emilia
Romagna"
locations[7] = "Via Roma, 40128, Granarolo Emilia,Bologna,
Emilia Romagna"


InfoString[0] = "Piazza Maggiore, 40128, Bologna, Emilia
Romagna"
InfoString[1] = "Vicolo San Giobbe, 40128, Bologna, Emilia
Romagna"
InfoString[2] = "Via Monte Grappa, 40128, Bologna, Emilia
Romagna"
InfoString[3] = "Via Barberia, 40128, Bologna, Emilia
Romagna"
InfoString[4] = "Via Riva di reno, 40128, Bologna, Emilia
Romagna"
InfoString[5] = "Via Marsala, 40128, Bologna, Emilia
Romagna"
InfoString[6] = "Via Bocca di Lupo, 40128, Bologna, Emilia
Romagna"
InfoString[7] = "Via Roma, 40128, Granarolo
Emilia,Bologna, Emilia Romagna"



//var test = findObjWithClientId("hd_desc");
//desc = test.value;
//InfoString = desc.split("||");

//var testimage = findObjWithClientId("hd_image");
//image = testimage.value;

//var TestzoomMap = findObjWithClientId("hd_zoom");
//zoomMap = parseInt(TestzoomMap.value);

var mapOpt = {
mapTypeId: google.maps.MapTypeId.ROADMAP
//center: new google.maps.LatLng(38.00, -100.00),
//zoom: 6
};

map = new
google.maps.Map(document.getElementById("map_canvas"), mapOpt);

geocoder = new google.maps.Geocoder();

}



geocoder.geocode({ 'address': locations[index] },
function(results, status) {
//document.write(status + " ; ");
if (status == google.maps.GeocoderStatus.OK) {
if (index == 0) {
map.setCenter(results[0].geometry.location);
map.setZoom(8);

Centro = results[0].geometry.location;

// Cerchio Sulla Mappa
var circle = new google.maps.Circle({
center: results[0].geometry.location,
map: map,
radius: 1000 // 1 km
});

CentroLat = results[0].geometry.location.Ja;

CentroLang = results[0].geometry.location.Ka;

}

// if (index != 0) {
StoreLat = results[0].geometry.location.Ja;
StoreLang = results[0].geometry.location.Ka;

var distanza;



distanza = distance(CentroLat, CentroLang, StoreLat,
StoreLang);

// }
//alert(distanza);

if (distanza < 30.00) {

//Creo la mia infowindows
var infowindow = new google.maps.InfoWindow({
content: InfoString[index]
});

if (image == "0") {
var marker = new google.maps.Marker
({ map: map,
position: results[0].geometry.location,
animation: google.maps.Animation.DROP,
title: "Imperial Store"
//icon: "images/fumetto.png"
});
};
if (image == "1") {
var marker = new google.maps.Marker
({ map: map,
position: results[0].geometry.location,
animation: google.maps.Animation.DROP,
title: "Imperial Headquarter -
Showroom"
//icon: "../../images/fumettoHQ.png"
});
};

//A Ogni ciclo assegno alla al mio Marker una
Infowindows che si aprirà al click del mouse
google.maps.event.addListener(marker, 'click',
function() {
infowindow.open(map, marker);
});



}

}

else {

alert(status + " alla posizione " + locations[index]);


}

// Call the geocoder with a 1000ms delay
index++;
if (locations.length > index) {
setTimeout(geocoderFunction, 1000);
}
});
if (index == locations.length) {

index = 0;


}
}



// funzione per calcolare la distanza in base alle coordinate
function distance(lat1,lon1,lat2,lon2) {


var dist;

var R = 6371; // km (change this constant to get miles)

var dLat = (lat2-lat1) * Math.PI / 180;

var dLon = (lon2-lon1) * Math.PI / 180;

var a = Math.sin(dLat/2) * Math.sin(dLat/2) +

Math.cos(lat1 * Math.PI / 180 ) * Math.cos(lat2 * Math.PI /
180 ) *

Math.sin(dLon/2) * Math.sin(dLon/2);

var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

var d = R * c;

// if (d>1) return Math.round(d)+"km";
// else if (d<=1) return Math.round(d*1000)+"m";

dist=(Math.round(d * 1000))/1000;



return dist;

}
Reply all
Reply to author
Forward
0 new messages