Problem:1- Hello, I'm developing a small javascript application get the addresses and Geocode it and save to database. I have almost 15000+ addresses which i want to get the lat/long of each. I'm iterating through loop, but the Geocoding function always returns undefined. Here is what i have tried.
InitDatabaseCities Function, which i uses to call.
function InitDatabaseCities()
{
var cit = CSVstring_to_Array();
for (var ase of cit)
{
var addrrs = ase.AREA + ', ' + ase.CITY;
var ltlg = GeoCoding(addrrs)
SaveToDb(ase.AREA, ase.CITY, ase.DIST_CENTER, ltlg.split(',')[0], ltlg.split(',')[1]);
}
}
GeoCoding Function
function GeoCoding(addr) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': addr }, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
var latlong = latitude + ',' + longitude;
console.log(latlong);
return latlong;
}
});
}
SaveToDb Function
function SaveToDb(area, city, dist_center, lat, lng)
{
//var cities = CSVstring_to_Array();
var url = "http://localhost:50264/api/service-areas/post";
var data = new FormData();
data.append('area', area);
data.append('city', city);
data.append('dist_center', dist_center);
data.append('lat', lat);
data.append('lng', lng);
$.ajax({
url: url,
type: "Post",
dataType: "json",
data: data,
success: function (response) {
alert(response);
},
error: function (xhr, status, err) {
}
});
}
CSVToArray Function
function CSVstring_to_Array(delimiter = ',') {
var data = `AREA,CITY,DIST_CENTER
FOREST CITY,ALTAMONTE,0
WEATHERSFIELD,ALTAMONTE,1
RIVERSIDE ACRES,ALTAMONTE,2
ORIENTA GARDENS,ALTAMONTE,2
SANLANDO SPRINGS,ALTAMONTE,2
WEKIVA SPRINGS,ALTAMONTE,2
CLERMONT,WINDERMERE,15
`;
// fetch("http://localhost:41847/media/cities.csv").then((r) => { r.text().then((d) => { data = d }) });
/* This variable will collect all the titles
from the data variable
["Name", "Roll Number"] */
const titles = data.slice(0, data
.indexOf('\n')).split(delimiter);
/* This variable will store the values
from the data
[ 'Rohan,01', 'Aryan,02' ] */
const titleValues = data.slice(data
.indexOf('\n') + 1).split('\n');
/* Map function will itterate over all
values of title values array and
append each object at the end of
the array */
const ansArray = titleValues.map(function (v) {
/* Values variable will store individual
title values
[ 'Rohan', '01' ] */
const values = v.split(delimiter);
/* storeKeyValue variable will store
object containing each title
with their respective values i.e
{ Name: 'Rohan', 'Roll Number': '01' } */
const storeKeyValue = titles.reduce(
function (obj, title, index) {
obj[title] = values[index];
return obj;
}, {});
return storeKeyValue;
});
return ansArray;
};
When i call GeoCoding() in console with address, it works fine, but it doesn't work in loop.
Problem:2- I have a list addresses 15000+ and i want to find the closest location from my current location. Like A,B,C,D,F,G are the list of addresses and I'm on Address Z, I want to know is there any better solution (instead of looping all one by one) that i can find which location is closest to Z address and what is the distance? Most likely Traveling Sale-man problem.