Javascript function not waiting for function to finish












0















I'm trying to write a map function that adds all the markers for locations. However, I've got one spot in my code in which I'm calling a function, but js is not waiting for the response before continuing to run code. Here's the script in it's entirety.



The problem in question is inside the getMap() function on this line of code



TrialLocationAddress = getTrialLocationGeoCode(TrialLocation.Address);



I need it to wait here until it gets the address properly before proceeding. I've tried inserting await but then it throws and error saying that its only async functions






<script type="text/javascript">
var MapID = "googleMap";
var defaultMapLocation = {
lat: 40.196409,
lon: -97.885575,
zoom: 4.75
};
var NewMapOptions;
var TrialLocations = [
{
Name: "Medicine",
Address: "Address 1, Abington, PA 19046"
},
{
Name: "Boston",
Address: "Address 2, Waltham, MA 02451"
}
];
var TrialLocationInfoWindows = ;

function initMap() {

var gGeoCoder;
var UserEnteredAddress = '<?php echo ( !empty( $UserEnteredAddress ) ? $UserEnteredAddress : "false"); ?>';
var MapPosition = new google.maps.LatLng(defaultMapLocation['lat'], defaultMapLocation['lon']);

if( UserEnteredAddress !== 'false' )
{
gGeoCoder = new google.maps.Geocoder();
if ( gGeoCoder ) {
gGeoCoder.geocode({
'address': UserEnteredAddress
}, function (results, status)
{
if ( status === google.maps.GeocoderStatus.OK ) {
if ( status !== google.maps.GeocoderStatus.ZERO_RESULTS ) {
NewMapOptions = {
zoom: 7,
center: results[0].geometry.location
};
getMap(NewMapOptions, results[0]);
} else {
alert("No location results found");
}
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
} else {
alert( "Geocode encountered an error initializing" );
}
} else {
NewMapOptions = {
zoom: defaultMapLocation['zoom'],
center: MapPosition
};
getMap( NewMapOptions );
}
}

function getMap( NewMap, SearchLocation = false ) {
var i = 0;
var map = new google.maps.Map(document.getElementById(MapID), NewMap);
var TrialLocationAddress;

if( SearchLocation !== false )
{
var infowindow = new google.maps.InfoWindow(
{
content: '<b>Your Location:</b> ' + SearchLocation.formatted_address,
size: new google.maps.Size(150, 50)
});

var searchMarker = new google.maps.Marker({
position: SearchLocation.geometry.location,
map: map,
title: SearchLocation.formatted_address,
icon:{
url: "http://maps.google.com/mapfiles/kml/pal2/icon13.png"
}
});

google.maps.event.addListener( searchMarker, 'click', function () {
infowindow.open(map, searchMarker);
});
}

TrialLocations.forEach(function(TrialLocation){
console.log( TrialLocation.Address );
TrialLocationAddress = getTrialLocationGeoCode(TrialLocation.Address);
console.log( "AFTER ADDRESS:" + TrialLocationAddress );
if( TrialLocationAddress !== false && typeof TrialLocationAddress != 'undefined') {
console.log("trial location address:" + TrialLocationAddress );
var TrialLocationMarker = new google.maps.Marker({
position: TrialLocationAddress.geometry.location,
map: map,
title: TrialLocation.Name
});

google.maps.event.addListener(TrialLocationMarker, 'click', function (TrialLocation, i) {
return function () {
var infowindow = new google.maps.InfoWindow(
{
content: '<b>' + TrialLocation.Name + '</b>',
size: new google.maps.Size(150, 50)
});
infowindow.open(map, TrialLocationMarker);
}
});
}
});
}

function getTrialLocationGeoCode( Address )
{
console.log ("init trial geo");
gGeoCoder = new google.maps.Geocoder();
if ( gGeoCoder ) {
gGeoCoder.geocode({
'address': Address
}, function (results, status)
{
console.log("inside the function for location geo");
if ( status === google.maps.GeocoderStatus.OK ) {
if ( status !== google.maps.GeocoderStatus.ZERO_RESULTS ) {
console.log("inside trialgeo" + results[0] );
return results[0];
} else {
console.log("error 1");
return false;
}
} else {
console.log("error 2");
return false;
}
});
} else {
console.log("error 3");
return false;
}
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key={APIKEY}&callback=initMap"></script>

<div id="googleMap" style="width: 100%; height:600px;"></div>

<?php
if( array_key_exists( "search", $_POST ) ):
$UserEnteredAddress = $_POST["search"];
elseif( array_key_exists( "search", $_GET ) ):
$UserEnteredAddress = $_GET["search"];
else:
$UserEnteredAddress = "";
endif;
?>












share|improve this question























  • Please read about asynchronous functions - eloquentjavascript.net/11_async.html

    – Abana Clara
    Nov 21 '18 at 0:49













  • it throws and error saying that its only async functions ??? what is the quote-unquote message and where/what line is it being thrown on?

    – radarbob
    Nov 21 '18 at 1:07











  • Video: Async/Await: Modern Concurrency In JavaScript. And, read MDN documentation

    – radarbob
    Nov 21 '18 at 1:18


















0















I'm trying to write a map function that adds all the markers for locations. However, I've got one spot in my code in which I'm calling a function, but js is not waiting for the response before continuing to run code. Here's the script in it's entirety.



The problem in question is inside the getMap() function on this line of code



TrialLocationAddress = getTrialLocationGeoCode(TrialLocation.Address);



I need it to wait here until it gets the address properly before proceeding. I've tried inserting await but then it throws and error saying that its only async functions






<script type="text/javascript">
var MapID = "googleMap";
var defaultMapLocation = {
lat: 40.196409,
lon: -97.885575,
zoom: 4.75
};
var NewMapOptions;
var TrialLocations = [
{
Name: "Medicine",
Address: "Address 1, Abington, PA 19046"
},
{
Name: "Boston",
Address: "Address 2, Waltham, MA 02451"
}
];
var TrialLocationInfoWindows = ;

function initMap() {

var gGeoCoder;
var UserEnteredAddress = '<?php echo ( !empty( $UserEnteredAddress ) ? $UserEnteredAddress : "false"); ?>';
var MapPosition = new google.maps.LatLng(defaultMapLocation['lat'], defaultMapLocation['lon']);

if( UserEnteredAddress !== 'false' )
{
gGeoCoder = new google.maps.Geocoder();
if ( gGeoCoder ) {
gGeoCoder.geocode({
'address': UserEnteredAddress
}, function (results, status)
{
if ( status === google.maps.GeocoderStatus.OK ) {
if ( status !== google.maps.GeocoderStatus.ZERO_RESULTS ) {
NewMapOptions = {
zoom: 7,
center: results[0].geometry.location
};
getMap(NewMapOptions, results[0]);
} else {
alert("No location results found");
}
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
} else {
alert( "Geocode encountered an error initializing" );
}
} else {
NewMapOptions = {
zoom: defaultMapLocation['zoom'],
center: MapPosition
};
getMap( NewMapOptions );
}
}

function getMap( NewMap, SearchLocation = false ) {
var i = 0;
var map = new google.maps.Map(document.getElementById(MapID), NewMap);
var TrialLocationAddress;

if( SearchLocation !== false )
{
var infowindow = new google.maps.InfoWindow(
{
content: '<b>Your Location:</b> ' + SearchLocation.formatted_address,
size: new google.maps.Size(150, 50)
});

var searchMarker = new google.maps.Marker({
position: SearchLocation.geometry.location,
map: map,
title: SearchLocation.formatted_address,
icon:{
url: "http://maps.google.com/mapfiles/kml/pal2/icon13.png"
}
});

google.maps.event.addListener( searchMarker, 'click', function () {
infowindow.open(map, searchMarker);
});
}

TrialLocations.forEach(function(TrialLocation){
console.log( TrialLocation.Address );
TrialLocationAddress = getTrialLocationGeoCode(TrialLocation.Address);
console.log( "AFTER ADDRESS:" + TrialLocationAddress );
if( TrialLocationAddress !== false && typeof TrialLocationAddress != 'undefined') {
console.log("trial location address:" + TrialLocationAddress );
var TrialLocationMarker = new google.maps.Marker({
position: TrialLocationAddress.geometry.location,
map: map,
title: TrialLocation.Name
});

google.maps.event.addListener(TrialLocationMarker, 'click', function (TrialLocation, i) {
return function () {
var infowindow = new google.maps.InfoWindow(
{
content: '<b>' + TrialLocation.Name + '</b>',
size: new google.maps.Size(150, 50)
});
infowindow.open(map, TrialLocationMarker);
}
});
}
});
}

function getTrialLocationGeoCode( Address )
{
console.log ("init trial geo");
gGeoCoder = new google.maps.Geocoder();
if ( gGeoCoder ) {
gGeoCoder.geocode({
'address': Address
}, function (results, status)
{
console.log("inside the function for location geo");
if ( status === google.maps.GeocoderStatus.OK ) {
if ( status !== google.maps.GeocoderStatus.ZERO_RESULTS ) {
console.log("inside trialgeo" + results[0] );
return results[0];
} else {
console.log("error 1");
return false;
}
} else {
console.log("error 2");
return false;
}
});
} else {
console.log("error 3");
return false;
}
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key={APIKEY}&callback=initMap"></script>

<div id="googleMap" style="width: 100%; height:600px;"></div>

<?php
if( array_key_exists( "search", $_POST ) ):
$UserEnteredAddress = $_POST["search"];
elseif( array_key_exists( "search", $_GET ) ):
$UserEnteredAddress = $_GET["search"];
else:
$UserEnteredAddress = "";
endif;
?>












share|improve this question























  • Please read about asynchronous functions - eloquentjavascript.net/11_async.html

    – Abana Clara
    Nov 21 '18 at 0:49













  • it throws and error saying that its only async functions ??? what is the quote-unquote message and where/what line is it being thrown on?

    – radarbob
    Nov 21 '18 at 1:07











  • Video: Async/Await: Modern Concurrency In JavaScript. And, read MDN documentation

    – radarbob
    Nov 21 '18 at 1:18
















0












0








0








I'm trying to write a map function that adds all the markers for locations. However, I've got one spot in my code in which I'm calling a function, but js is not waiting for the response before continuing to run code. Here's the script in it's entirety.



The problem in question is inside the getMap() function on this line of code



TrialLocationAddress = getTrialLocationGeoCode(TrialLocation.Address);



I need it to wait here until it gets the address properly before proceeding. I've tried inserting await but then it throws and error saying that its only async functions






<script type="text/javascript">
var MapID = "googleMap";
var defaultMapLocation = {
lat: 40.196409,
lon: -97.885575,
zoom: 4.75
};
var NewMapOptions;
var TrialLocations = [
{
Name: "Medicine",
Address: "Address 1, Abington, PA 19046"
},
{
Name: "Boston",
Address: "Address 2, Waltham, MA 02451"
}
];
var TrialLocationInfoWindows = ;

function initMap() {

var gGeoCoder;
var UserEnteredAddress = '<?php echo ( !empty( $UserEnteredAddress ) ? $UserEnteredAddress : "false"); ?>';
var MapPosition = new google.maps.LatLng(defaultMapLocation['lat'], defaultMapLocation['lon']);

if( UserEnteredAddress !== 'false' )
{
gGeoCoder = new google.maps.Geocoder();
if ( gGeoCoder ) {
gGeoCoder.geocode({
'address': UserEnteredAddress
}, function (results, status)
{
if ( status === google.maps.GeocoderStatus.OK ) {
if ( status !== google.maps.GeocoderStatus.ZERO_RESULTS ) {
NewMapOptions = {
zoom: 7,
center: results[0].geometry.location
};
getMap(NewMapOptions, results[0]);
} else {
alert("No location results found");
}
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
} else {
alert( "Geocode encountered an error initializing" );
}
} else {
NewMapOptions = {
zoom: defaultMapLocation['zoom'],
center: MapPosition
};
getMap( NewMapOptions );
}
}

function getMap( NewMap, SearchLocation = false ) {
var i = 0;
var map = new google.maps.Map(document.getElementById(MapID), NewMap);
var TrialLocationAddress;

if( SearchLocation !== false )
{
var infowindow = new google.maps.InfoWindow(
{
content: '<b>Your Location:</b> ' + SearchLocation.formatted_address,
size: new google.maps.Size(150, 50)
});

var searchMarker = new google.maps.Marker({
position: SearchLocation.geometry.location,
map: map,
title: SearchLocation.formatted_address,
icon:{
url: "http://maps.google.com/mapfiles/kml/pal2/icon13.png"
}
});

google.maps.event.addListener( searchMarker, 'click', function () {
infowindow.open(map, searchMarker);
});
}

TrialLocations.forEach(function(TrialLocation){
console.log( TrialLocation.Address );
TrialLocationAddress = getTrialLocationGeoCode(TrialLocation.Address);
console.log( "AFTER ADDRESS:" + TrialLocationAddress );
if( TrialLocationAddress !== false && typeof TrialLocationAddress != 'undefined') {
console.log("trial location address:" + TrialLocationAddress );
var TrialLocationMarker = new google.maps.Marker({
position: TrialLocationAddress.geometry.location,
map: map,
title: TrialLocation.Name
});

google.maps.event.addListener(TrialLocationMarker, 'click', function (TrialLocation, i) {
return function () {
var infowindow = new google.maps.InfoWindow(
{
content: '<b>' + TrialLocation.Name + '</b>',
size: new google.maps.Size(150, 50)
});
infowindow.open(map, TrialLocationMarker);
}
});
}
});
}

function getTrialLocationGeoCode( Address )
{
console.log ("init trial geo");
gGeoCoder = new google.maps.Geocoder();
if ( gGeoCoder ) {
gGeoCoder.geocode({
'address': Address
}, function (results, status)
{
console.log("inside the function for location geo");
if ( status === google.maps.GeocoderStatus.OK ) {
if ( status !== google.maps.GeocoderStatus.ZERO_RESULTS ) {
console.log("inside trialgeo" + results[0] );
return results[0];
} else {
console.log("error 1");
return false;
}
} else {
console.log("error 2");
return false;
}
});
} else {
console.log("error 3");
return false;
}
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key={APIKEY}&callback=initMap"></script>

<div id="googleMap" style="width: 100%; height:600px;"></div>

<?php
if( array_key_exists( "search", $_POST ) ):
$UserEnteredAddress = $_POST["search"];
elseif( array_key_exists( "search", $_GET ) ):
$UserEnteredAddress = $_GET["search"];
else:
$UserEnteredAddress = "";
endif;
?>












share|improve this question














I'm trying to write a map function that adds all the markers for locations. However, I've got one spot in my code in which I'm calling a function, but js is not waiting for the response before continuing to run code. Here's the script in it's entirety.



The problem in question is inside the getMap() function on this line of code



TrialLocationAddress = getTrialLocationGeoCode(TrialLocation.Address);



I need it to wait here until it gets the address properly before proceeding. I've tried inserting await but then it throws and error saying that its only async functions






<script type="text/javascript">
var MapID = "googleMap";
var defaultMapLocation = {
lat: 40.196409,
lon: -97.885575,
zoom: 4.75
};
var NewMapOptions;
var TrialLocations = [
{
Name: "Medicine",
Address: "Address 1, Abington, PA 19046"
},
{
Name: "Boston",
Address: "Address 2, Waltham, MA 02451"
}
];
var TrialLocationInfoWindows = ;

function initMap() {

var gGeoCoder;
var UserEnteredAddress = '<?php echo ( !empty( $UserEnteredAddress ) ? $UserEnteredAddress : "false"); ?>';
var MapPosition = new google.maps.LatLng(defaultMapLocation['lat'], defaultMapLocation['lon']);

if( UserEnteredAddress !== 'false' )
{
gGeoCoder = new google.maps.Geocoder();
if ( gGeoCoder ) {
gGeoCoder.geocode({
'address': UserEnteredAddress
}, function (results, status)
{
if ( status === google.maps.GeocoderStatus.OK ) {
if ( status !== google.maps.GeocoderStatus.ZERO_RESULTS ) {
NewMapOptions = {
zoom: 7,
center: results[0].geometry.location
};
getMap(NewMapOptions, results[0]);
} else {
alert("No location results found");
}
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
} else {
alert( "Geocode encountered an error initializing" );
}
} else {
NewMapOptions = {
zoom: defaultMapLocation['zoom'],
center: MapPosition
};
getMap( NewMapOptions );
}
}

function getMap( NewMap, SearchLocation = false ) {
var i = 0;
var map = new google.maps.Map(document.getElementById(MapID), NewMap);
var TrialLocationAddress;

if( SearchLocation !== false )
{
var infowindow = new google.maps.InfoWindow(
{
content: '<b>Your Location:</b> ' + SearchLocation.formatted_address,
size: new google.maps.Size(150, 50)
});

var searchMarker = new google.maps.Marker({
position: SearchLocation.geometry.location,
map: map,
title: SearchLocation.formatted_address,
icon:{
url: "http://maps.google.com/mapfiles/kml/pal2/icon13.png"
}
});

google.maps.event.addListener( searchMarker, 'click', function () {
infowindow.open(map, searchMarker);
});
}

TrialLocations.forEach(function(TrialLocation){
console.log( TrialLocation.Address );
TrialLocationAddress = getTrialLocationGeoCode(TrialLocation.Address);
console.log( "AFTER ADDRESS:" + TrialLocationAddress );
if( TrialLocationAddress !== false && typeof TrialLocationAddress != 'undefined') {
console.log("trial location address:" + TrialLocationAddress );
var TrialLocationMarker = new google.maps.Marker({
position: TrialLocationAddress.geometry.location,
map: map,
title: TrialLocation.Name
});

google.maps.event.addListener(TrialLocationMarker, 'click', function (TrialLocation, i) {
return function () {
var infowindow = new google.maps.InfoWindow(
{
content: '<b>' + TrialLocation.Name + '</b>',
size: new google.maps.Size(150, 50)
});
infowindow.open(map, TrialLocationMarker);
}
});
}
});
}

function getTrialLocationGeoCode( Address )
{
console.log ("init trial geo");
gGeoCoder = new google.maps.Geocoder();
if ( gGeoCoder ) {
gGeoCoder.geocode({
'address': Address
}, function (results, status)
{
console.log("inside the function for location geo");
if ( status === google.maps.GeocoderStatus.OK ) {
if ( status !== google.maps.GeocoderStatus.ZERO_RESULTS ) {
console.log("inside trialgeo" + results[0] );
return results[0];
} else {
console.log("error 1");
return false;
}
} else {
console.log("error 2");
return false;
}
});
} else {
console.log("error 3");
return false;
}
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key={APIKEY}&callback=initMap"></script>

<div id="googleMap" style="width: 100%; height:600px;"></div>

<?php
if( array_key_exists( "search", $_POST ) ):
$UserEnteredAddress = $_POST["search"];
elseif( array_key_exists( "search", $_GET ) ):
$UserEnteredAddress = $_GET["search"];
else:
$UserEnteredAddress = "";
endif;
?>








<script type="text/javascript">
var MapID = "googleMap";
var defaultMapLocation = {
lat: 40.196409,
lon: -97.885575,
zoom: 4.75
};
var NewMapOptions;
var TrialLocations = [
{
Name: "Medicine",
Address: "Address 1, Abington, PA 19046"
},
{
Name: "Boston",
Address: "Address 2, Waltham, MA 02451"
}
];
var TrialLocationInfoWindows = ;

function initMap() {

var gGeoCoder;
var UserEnteredAddress = '<?php echo ( !empty( $UserEnteredAddress ) ? $UserEnteredAddress : "false"); ?>';
var MapPosition = new google.maps.LatLng(defaultMapLocation['lat'], defaultMapLocation['lon']);

if( UserEnteredAddress !== 'false' )
{
gGeoCoder = new google.maps.Geocoder();
if ( gGeoCoder ) {
gGeoCoder.geocode({
'address': UserEnteredAddress
}, function (results, status)
{
if ( status === google.maps.GeocoderStatus.OK ) {
if ( status !== google.maps.GeocoderStatus.ZERO_RESULTS ) {
NewMapOptions = {
zoom: 7,
center: results[0].geometry.location
};
getMap(NewMapOptions, results[0]);
} else {
alert("No location results found");
}
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
} else {
alert( "Geocode encountered an error initializing" );
}
} else {
NewMapOptions = {
zoom: defaultMapLocation['zoom'],
center: MapPosition
};
getMap( NewMapOptions );
}
}

function getMap( NewMap, SearchLocation = false ) {
var i = 0;
var map = new google.maps.Map(document.getElementById(MapID), NewMap);
var TrialLocationAddress;

if( SearchLocation !== false )
{
var infowindow = new google.maps.InfoWindow(
{
content: '<b>Your Location:</b> ' + SearchLocation.formatted_address,
size: new google.maps.Size(150, 50)
});

var searchMarker = new google.maps.Marker({
position: SearchLocation.geometry.location,
map: map,
title: SearchLocation.formatted_address,
icon:{
url: "http://maps.google.com/mapfiles/kml/pal2/icon13.png"
}
});

google.maps.event.addListener( searchMarker, 'click', function () {
infowindow.open(map, searchMarker);
});
}

TrialLocations.forEach(function(TrialLocation){
console.log( TrialLocation.Address );
TrialLocationAddress = getTrialLocationGeoCode(TrialLocation.Address);
console.log( "AFTER ADDRESS:" + TrialLocationAddress );
if( TrialLocationAddress !== false && typeof TrialLocationAddress != 'undefined') {
console.log("trial location address:" + TrialLocationAddress );
var TrialLocationMarker = new google.maps.Marker({
position: TrialLocationAddress.geometry.location,
map: map,
title: TrialLocation.Name
});

google.maps.event.addListener(TrialLocationMarker, 'click', function (TrialLocation, i) {
return function () {
var infowindow = new google.maps.InfoWindow(
{
content: '<b>' + TrialLocation.Name + '</b>',
size: new google.maps.Size(150, 50)
});
infowindow.open(map, TrialLocationMarker);
}
});
}
});
}

function getTrialLocationGeoCode( Address )
{
console.log ("init trial geo");
gGeoCoder = new google.maps.Geocoder();
if ( gGeoCoder ) {
gGeoCoder.geocode({
'address': Address
}, function (results, status)
{
console.log("inside the function for location geo");
if ( status === google.maps.GeocoderStatus.OK ) {
if ( status !== google.maps.GeocoderStatus.ZERO_RESULTS ) {
console.log("inside trialgeo" + results[0] );
return results[0];
} else {
console.log("error 1");
return false;
}
} else {
console.log("error 2");
return false;
}
});
} else {
console.log("error 3");
return false;
}
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key={APIKEY}&callback=initMap"></script>

<div id="googleMap" style="width: 100%; height:600px;"></div>

<?php
if( array_key_exists( "search", $_POST ) ):
$UserEnteredAddress = $_POST["search"];
elseif( array_key_exists( "search", $_GET ) ):
$UserEnteredAddress = $_GET["search"];
else:
$UserEnteredAddress = "";
endif;
?>





<script type="text/javascript">
var MapID = "googleMap";
var defaultMapLocation = {
lat: 40.196409,
lon: -97.885575,
zoom: 4.75
};
var NewMapOptions;
var TrialLocations = [
{
Name: "Medicine",
Address: "Address 1, Abington, PA 19046"
},
{
Name: "Boston",
Address: "Address 2, Waltham, MA 02451"
}
];
var TrialLocationInfoWindows = ;

function initMap() {

var gGeoCoder;
var UserEnteredAddress = '<?php echo ( !empty( $UserEnteredAddress ) ? $UserEnteredAddress : "false"); ?>';
var MapPosition = new google.maps.LatLng(defaultMapLocation['lat'], defaultMapLocation['lon']);

if( UserEnteredAddress !== 'false' )
{
gGeoCoder = new google.maps.Geocoder();
if ( gGeoCoder ) {
gGeoCoder.geocode({
'address': UserEnteredAddress
}, function (results, status)
{
if ( status === google.maps.GeocoderStatus.OK ) {
if ( status !== google.maps.GeocoderStatus.ZERO_RESULTS ) {
NewMapOptions = {
zoom: 7,
center: results[0].geometry.location
};
getMap(NewMapOptions, results[0]);
} else {
alert("No location results found");
}
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
} else {
alert( "Geocode encountered an error initializing" );
}
} else {
NewMapOptions = {
zoom: defaultMapLocation['zoom'],
center: MapPosition
};
getMap( NewMapOptions );
}
}

function getMap( NewMap, SearchLocation = false ) {
var i = 0;
var map = new google.maps.Map(document.getElementById(MapID), NewMap);
var TrialLocationAddress;

if( SearchLocation !== false )
{
var infowindow = new google.maps.InfoWindow(
{
content: '<b>Your Location:</b> ' + SearchLocation.formatted_address,
size: new google.maps.Size(150, 50)
});

var searchMarker = new google.maps.Marker({
position: SearchLocation.geometry.location,
map: map,
title: SearchLocation.formatted_address,
icon:{
url: "http://maps.google.com/mapfiles/kml/pal2/icon13.png"
}
});

google.maps.event.addListener( searchMarker, 'click', function () {
infowindow.open(map, searchMarker);
});
}

TrialLocations.forEach(function(TrialLocation){
console.log( TrialLocation.Address );
TrialLocationAddress = getTrialLocationGeoCode(TrialLocation.Address);
console.log( "AFTER ADDRESS:" + TrialLocationAddress );
if( TrialLocationAddress !== false && typeof TrialLocationAddress != 'undefined') {
console.log("trial location address:" + TrialLocationAddress );
var TrialLocationMarker = new google.maps.Marker({
position: TrialLocationAddress.geometry.location,
map: map,
title: TrialLocation.Name
});

google.maps.event.addListener(TrialLocationMarker, 'click', function (TrialLocation, i) {
return function () {
var infowindow = new google.maps.InfoWindow(
{
content: '<b>' + TrialLocation.Name + '</b>',
size: new google.maps.Size(150, 50)
});
infowindow.open(map, TrialLocationMarker);
}
});
}
});
}

function getTrialLocationGeoCode( Address )
{
console.log ("init trial geo");
gGeoCoder = new google.maps.Geocoder();
if ( gGeoCoder ) {
gGeoCoder.geocode({
'address': Address
}, function (results, status)
{
console.log("inside the function for location geo");
if ( status === google.maps.GeocoderStatus.OK ) {
if ( status !== google.maps.GeocoderStatus.ZERO_RESULTS ) {
console.log("inside trialgeo" + results[0] );
return results[0];
} else {
console.log("error 1");
return false;
}
} else {
console.log("error 2");
return false;
}
});
} else {
console.log("error 3");
return false;
}
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key={APIKEY}&callback=initMap"></script>

<div id="googleMap" style="width: 100%; height:600px;"></div>

<?php
if( array_key_exists( "search", $_POST ) ):
$UserEnteredAddress = $_POST["search"];
elseif( array_key_exists( "search", $_GET ) ):
$UserEnteredAddress = $_GET["search"];
else:
$UserEnteredAddress = "";
endif;
?>






javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 0:38









joebjoeb

346218




346218













  • Please read about asynchronous functions - eloquentjavascript.net/11_async.html

    – Abana Clara
    Nov 21 '18 at 0:49













  • it throws and error saying that its only async functions ??? what is the quote-unquote message and where/what line is it being thrown on?

    – radarbob
    Nov 21 '18 at 1:07











  • Video: Async/Await: Modern Concurrency In JavaScript. And, read MDN documentation

    – radarbob
    Nov 21 '18 at 1:18





















  • Please read about asynchronous functions - eloquentjavascript.net/11_async.html

    – Abana Clara
    Nov 21 '18 at 0:49













  • it throws and error saying that its only async functions ??? what is the quote-unquote message and where/what line is it being thrown on?

    – radarbob
    Nov 21 '18 at 1:07











  • Video: Async/Await: Modern Concurrency In JavaScript. And, read MDN documentation

    – radarbob
    Nov 21 '18 at 1:18



















Please read about asynchronous functions - eloquentjavascript.net/11_async.html

– Abana Clara
Nov 21 '18 at 0:49







Please read about asynchronous functions - eloquentjavascript.net/11_async.html

– Abana Clara
Nov 21 '18 at 0:49















it throws and error saying that its only async functions ??? what is the quote-unquote message and where/what line is it being thrown on?

– radarbob
Nov 21 '18 at 1:07





it throws and error saying that its only async functions ??? what is the quote-unquote message and where/what line is it being thrown on?

– radarbob
Nov 21 '18 at 1:07













Video: Async/Await: Modern Concurrency In JavaScript. And, read MDN documentation

– radarbob
Nov 21 '18 at 1:18







Video: Async/Await: Modern Concurrency In JavaScript. And, read MDN documentation

– radarbob
Nov 21 '18 at 1:18














2 Answers
2






active

oldest

votes


















1














you can go around this through two methods:



1 replace map with for-loop



2 add a counter that checks if every element has been iterated by the call back function.






share|improve this answer
























  • can you give me an example

    – joeb
    Nov 21 '18 at 0:52











  • im using mobile, so can't code right now. just search Internet using two solutions you'll find it, quite easily.

    – akhilesh
    Nov 21 '18 at 0:52








  • 1





    bad advice. keep the map function and just pass the iterator as the second param if you really need it (even though this answer isn't the solution to to issue)

    – acaputo
    Nov 21 '18 at 1:16













  • got it working now. I used a callback as suggested. I'm posting my fix above

    – joeb
    Nov 21 '18 at 1:25



















0














I got it working by adding a callback at the same location that was throwing the error. Thanks to akhilesh. Hope this helps someone else.






function getMap( NewMap, SearchLocation = false ) {
var i = 0;
var map = new google.maps.Map(document.getElementById(MapID), NewMap);
var TrialLocationAddress;

if( SearchLocation !== false )
{
var infowindow = new google.maps.InfoWindow(
{
content: '<b>Your Location:</b> ' + SearchLocation.formatted_address,
size: new google.maps.Size(150, 50)
});

var searchMarker = new google.maps.Marker({
position: SearchLocation.geometry.location,
map: map,
title: SearchLocation.formatted_address,
icon:{
url: "http://maps.google.com/mapfiles/kml/pal2/icon13.png"
}
});

google.maps.event.addListener( searchMarker, 'click', function () {
infowindow.open(map, searchMarker);
});
}

TrialLocations.forEach(function(TrialLocation){
console.log( TrialLocation.Address );
getTrialLocationGeoCode(TrialLocation, function( pGeo, pLocation ){
if( pGeo !== false && typeof pGeo != 'undefined') {
var TrialLocationMarker = new google.maps.Marker({
position: pGeo.geometry.location,
map: map,
title: pLocation.Name
});
google.maps.event.addListener(TrialLocationMarker, 'click', function (TrialLocation, i) {
return function () {
var infowindow = new google.maps.InfoWindow(
{
content: '<b>' + pLocation.Name + '</b>',
size: new google.maps.Size(150, 50)
});
infowindow.open(map, TrialLocationMarker);
}
});
}
});


});
}

function getTrialLocationGeoCode( pLocation, callback )
{
console.log ("init trial geo");
gGeoCoder = new google.maps.Geocoder();
if ( gGeoCoder ) {
gGeoCoder.geocode({
'address': pLocation.Address
}, function (results, status)
{
console.log("inside the function for location geo");
if ( status === google.maps.GeocoderStatus.OK ) {
if ( status !== google.maps.GeocoderStatus.ZERO_RESULTS ) {
console.log("inside trialgeo" + results[0] );
callback( results[0] , pLocation);
} else {
console.log("error 1");
return false;
}
} else {
console.log("error 2");
return false;
}
});
} else {
console.log("error 3");
return false;
}
}








share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403717%2fjavascript-function-not-waiting-for-function-to-finish%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    you can go around this through two methods:



    1 replace map with for-loop



    2 add a counter that checks if every element has been iterated by the call back function.






    share|improve this answer
























    • can you give me an example

      – joeb
      Nov 21 '18 at 0:52











    • im using mobile, so can't code right now. just search Internet using two solutions you'll find it, quite easily.

      – akhilesh
      Nov 21 '18 at 0:52








    • 1





      bad advice. keep the map function and just pass the iterator as the second param if you really need it (even though this answer isn't the solution to to issue)

      – acaputo
      Nov 21 '18 at 1:16













    • got it working now. I used a callback as suggested. I'm posting my fix above

      – joeb
      Nov 21 '18 at 1:25
















    1














    you can go around this through two methods:



    1 replace map with for-loop



    2 add a counter that checks if every element has been iterated by the call back function.






    share|improve this answer
























    • can you give me an example

      – joeb
      Nov 21 '18 at 0:52











    • im using mobile, so can't code right now. just search Internet using two solutions you'll find it, quite easily.

      – akhilesh
      Nov 21 '18 at 0:52








    • 1





      bad advice. keep the map function and just pass the iterator as the second param if you really need it (even though this answer isn't the solution to to issue)

      – acaputo
      Nov 21 '18 at 1:16













    • got it working now. I used a callback as suggested. I'm posting my fix above

      – joeb
      Nov 21 '18 at 1:25














    1












    1








    1







    you can go around this through two methods:



    1 replace map with for-loop



    2 add a counter that checks if every element has been iterated by the call back function.






    share|improve this answer













    you can go around this through two methods:



    1 replace map with for-loop



    2 add a counter that checks if every element has been iterated by the call back function.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 21 '18 at 0:50









    akhileshakhilesh

    258




    258













    • can you give me an example

      – joeb
      Nov 21 '18 at 0:52











    • im using mobile, so can't code right now. just search Internet using two solutions you'll find it, quite easily.

      – akhilesh
      Nov 21 '18 at 0:52








    • 1





      bad advice. keep the map function and just pass the iterator as the second param if you really need it (even though this answer isn't the solution to to issue)

      – acaputo
      Nov 21 '18 at 1:16













    • got it working now. I used a callback as suggested. I'm posting my fix above

      – joeb
      Nov 21 '18 at 1:25



















    • can you give me an example

      – joeb
      Nov 21 '18 at 0:52











    • im using mobile, so can't code right now. just search Internet using two solutions you'll find it, quite easily.

      – akhilesh
      Nov 21 '18 at 0:52








    • 1





      bad advice. keep the map function and just pass the iterator as the second param if you really need it (even though this answer isn't the solution to to issue)

      – acaputo
      Nov 21 '18 at 1:16













    • got it working now. I used a callback as suggested. I'm posting my fix above

      – joeb
      Nov 21 '18 at 1:25

















    can you give me an example

    – joeb
    Nov 21 '18 at 0:52





    can you give me an example

    – joeb
    Nov 21 '18 at 0:52













    im using mobile, so can't code right now. just search Internet using two solutions you'll find it, quite easily.

    – akhilesh
    Nov 21 '18 at 0:52







    im using mobile, so can't code right now. just search Internet using two solutions you'll find it, quite easily.

    – akhilesh
    Nov 21 '18 at 0:52






    1




    1





    bad advice. keep the map function and just pass the iterator as the second param if you really need it (even though this answer isn't the solution to to issue)

    – acaputo
    Nov 21 '18 at 1:16







    bad advice. keep the map function and just pass the iterator as the second param if you really need it (even though this answer isn't the solution to to issue)

    – acaputo
    Nov 21 '18 at 1:16















    got it working now. I used a callback as suggested. I'm posting my fix above

    – joeb
    Nov 21 '18 at 1:25





    got it working now. I used a callback as suggested. I'm posting my fix above

    – joeb
    Nov 21 '18 at 1:25













    0














    I got it working by adding a callback at the same location that was throwing the error. Thanks to akhilesh. Hope this helps someone else.






    function getMap( NewMap, SearchLocation = false ) {
    var i = 0;
    var map = new google.maps.Map(document.getElementById(MapID), NewMap);
    var TrialLocationAddress;

    if( SearchLocation !== false )
    {
    var infowindow = new google.maps.InfoWindow(
    {
    content: '<b>Your Location:</b> ' + SearchLocation.formatted_address,
    size: new google.maps.Size(150, 50)
    });

    var searchMarker = new google.maps.Marker({
    position: SearchLocation.geometry.location,
    map: map,
    title: SearchLocation.formatted_address,
    icon:{
    url: "http://maps.google.com/mapfiles/kml/pal2/icon13.png"
    }
    });

    google.maps.event.addListener( searchMarker, 'click', function () {
    infowindow.open(map, searchMarker);
    });
    }

    TrialLocations.forEach(function(TrialLocation){
    console.log( TrialLocation.Address );
    getTrialLocationGeoCode(TrialLocation, function( pGeo, pLocation ){
    if( pGeo !== false && typeof pGeo != 'undefined') {
    var TrialLocationMarker = new google.maps.Marker({
    position: pGeo.geometry.location,
    map: map,
    title: pLocation.Name
    });
    google.maps.event.addListener(TrialLocationMarker, 'click', function (TrialLocation, i) {
    return function () {
    var infowindow = new google.maps.InfoWindow(
    {
    content: '<b>' + pLocation.Name + '</b>',
    size: new google.maps.Size(150, 50)
    });
    infowindow.open(map, TrialLocationMarker);
    }
    });
    }
    });


    });
    }

    function getTrialLocationGeoCode( pLocation, callback )
    {
    console.log ("init trial geo");
    gGeoCoder = new google.maps.Geocoder();
    if ( gGeoCoder ) {
    gGeoCoder.geocode({
    'address': pLocation.Address
    }, function (results, status)
    {
    console.log("inside the function for location geo");
    if ( status === google.maps.GeocoderStatus.OK ) {
    if ( status !== google.maps.GeocoderStatus.ZERO_RESULTS ) {
    console.log("inside trialgeo" + results[0] );
    callback( results[0] , pLocation);
    } else {
    console.log("error 1");
    return false;
    }
    } else {
    console.log("error 2");
    return false;
    }
    });
    } else {
    console.log("error 3");
    return false;
    }
    }








    share|improve this answer




























      0














      I got it working by adding a callback at the same location that was throwing the error. Thanks to akhilesh. Hope this helps someone else.






      function getMap( NewMap, SearchLocation = false ) {
      var i = 0;
      var map = new google.maps.Map(document.getElementById(MapID), NewMap);
      var TrialLocationAddress;

      if( SearchLocation !== false )
      {
      var infowindow = new google.maps.InfoWindow(
      {
      content: '<b>Your Location:</b> ' + SearchLocation.formatted_address,
      size: new google.maps.Size(150, 50)
      });

      var searchMarker = new google.maps.Marker({
      position: SearchLocation.geometry.location,
      map: map,
      title: SearchLocation.formatted_address,
      icon:{
      url: "http://maps.google.com/mapfiles/kml/pal2/icon13.png"
      }
      });

      google.maps.event.addListener( searchMarker, 'click', function () {
      infowindow.open(map, searchMarker);
      });
      }

      TrialLocations.forEach(function(TrialLocation){
      console.log( TrialLocation.Address );
      getTrialLocationGeoCode(TrialLocation, function( pGeo, pLocation ){
      if( pGeo !== false && typeof pGeo != 'undefined') {
      var TrialLocationMarker = new google.maps.Marker({
      position: pGeo.geometry.location,
      map: map,
      title: pLocation.Name
      });
      google.maps.event.addListener(TrialLocationMarker, 'click', function (TrialLocation, i) {
      return function () {
      var infowindow = new google.maps.InfoWindow(
      {
      content: '<b>' + pLocation.Name + '</b>',
      size: new google.maps.Size(150, 50)
      });
      infowindow.open(map, TrialLocationMarker);
      }
      });
      }
      });


      });
      }

      function getTrialLocationGeoCode( pLocation, callback )
      {
      console.log ("init trial geo");
      gGeoCoder = new google.maps.Geocoder();
      if ( gGeoCoder ) {
      gGeoCoder.geocode({
      'address': pLocation.Address
      }, function (results, status)
      {
      console.log("inside the function for location geo");
      if ( status === google.maps.GeocoderStatus.OK ) {
      if ( status !== google.maps.GeocoderStatus.ZERO_RESULTS ) {
      console.log("inside trialgeo" + results[0] );
      callback( results[0] , pLocation);
      } else {
      console.log("error 1");
      return false;
      }
      } else {
      console.log("error 2");
      return false;
      }
      });
      } else {
      console.log("error 3");
      return false;
      }
      }








      share|improve this answer


























        0












        0








        0







        I got it working by adding a callback at the same location that was throwing the error. Thanks to akhilesh. Hope this helps someone else.






        function getMap( NewMap, SearchLocation = false ) {
        var i = 0;
        var map = new google.maps.Map(document.getElementById(MapID), NewMap);
        var TrialLocationAddress;

        if( SearchLocation !== false )
        {
        var infowindow = new google.maps.InfoWindow(
        {
        content: '<b>Your Location:</b> ' + SearchLocation.formatted_address,
        size: new google.maps.Size(150, 50)
        });

        var searchMarker = new google.maps.Marker({
        position: SearchLocation.geometry.location,
        map: map,
        title: SearchLocation.formatted_address,
        icon:{
        url: "http://maps.google.com/mapfiles/kml/pal2/icon13.png"
        }
        });

        google.maps.event.addListener( searchMarker, 'click', function () {
        infowindow.open(map, searchMarker);
        });
        }

        TrialLocations.forEach(function(TrialLocation){
        console.log( TrialLocation.Address );
        getTrialLocationGeoCode(TrialLocation, function( pGeo, pLocation ){
        if( pGeo !== false && typeof pGeo != 'undefined') {
        var TrialLocationMarker = new google.maps.Marker({
        position: pGeo.geometry.location,
        map: map,
        title: pLocation.Name
        });
        google.maps.event.addListener(TrialLocationMarker, 'click', function (TrialLocation, i) {
        return function () {
        var infowindow = new google.maps.InfoWindow(
        {
        content: '<b>' + pLocation.Name + '</b>',
        size: new google.maps.Size(150, 50)
        });
        infowindow.open(map, TrialLocationMarker);
        }
        });
        }
        });


        });
        }

        function getTrialLocationGeoCode( pLocation, callback )
        {
        console.log ("init trial geo");
        gGeoCoder = new google.maps.Geocoder();
        if ( gGeoCoder ) {
        gGeoCoder.geocode({
        'address': pLocation.Address
        }, function (results, status)
        {
        console.log("inside the function for location geo");
        if ( status === google.maps.GeocoderStatus.OK ) {
        if ( status !== google.maps.GeocoderStatus.ZERO_RESULTS ) {
        console.log("inside trialgeo" + results[0] );
        callback( results[0] , pLocation);
        } else {
        console.log("error 1");
        return false;
        }
        } else {
        console.log("error 2");
        return false;
        }
        });
        } else {
        console.log("error 3");
        return false;
        }
        }








        share|improve this answer













        I got it working by adding a callback at the same location that was throwing the error. Thanks to akhilesh. Hope this helps someone else.






        function getMap( NewMap, SearchLocation = false ) {
        var i = 0;
        var map = new google.maps.Map(document.getElementById(MapID), NewMap);
        var TrialLocationAddress;

        if( SearchLocation !== false )
        {
        var infowindow = new google.maps.InfoWindow(
        {
        content: '<b>Your Location:</b> ' + SearchLocation.formatted_address,
        size: new google.maps.Size(150, 50)
        });

        var searchMarker = new google.maps.Marker({
        position: SearchLocation.geometry.location,
        map: map,
        title: SearchLocation.formatted_address,
        icon:{
        url: "http://maps.google.com/mapfiles/kml/pal2/icon13.png"
        }
        });

        google.maps.event.addListener( searchMarker, 'click', function () {
        infowindow.open(map, searchMarker);
        });
        }

        TrialLocations.forEach(function(TrialLocation){
        console.log( TrialLocation.Address );
        getTrialLocationGeoCode(TrialLocation, function( pGeo, pLocation ){
        if( pGeo !== false && typeof pGeo != 'undefined') {
        var TrialLocationMarker = new google.maps.Marker({
        position: pGeo.geometry.location,
        map: map,
        title: pLocation.Name
        });
        google.maps.event.addListener(TrialLocationMarker, 'click', function (TrialLocation, i) {
        return function () {
        var infowindow = new google.maps.InfoWindow(
        {
        content: '<b>' + pLocation.Name + '</b>',
        size: new google.maps.Size(150, 50)
        });
        infowindow.open(map, TrialLocationMarker);
        }
        });
        }
        });


        });
        }

        function getTrialLocationGeoCode( pLocation, callback )
        {
        console.log ("init trial geo");
        gGeoCoder = new google.maps.Geocoder();
        if ( gGeoCoder ) {
        gGeoCoder.geocode({
        'address': pLocation.Address
        }, function (results, status)
        {
        console.log("inside the function for location geo");
        if ( status === google.maps.GeocoderStatus.OK ) {
        if ( status !== google.maps.GeocoderStatus.ZERO_RESULTS ) {
        console.log("inside trialgeo" + results[0] );
        callback( results[0] , pLocation);
        } else {
        console.log("error 1");
        return false;
        }
        } else {
        console.log("error 2");
        return false;
        }
        });
        } else {
        console.log("error 3");
        return false;
        }
        }








        function getMap( NewMap, SearchLocation = false ) {
        var i = 0;
        var map = new google.maps.Map(document.getElementById(MapID), NewMap);
        var TrialLocationAddress;

        if( SearchLocation !== false )
        {
        var infowindow = new google.maps.InfoWindow(
        {
        content: '<b>Your Location:</b> ' + SearchLocation.formatted_address,
        size: new google.maps.Size(150, 50)
        });

        var searchMarker = new google.maps.Marker({
        position: SearchLocation.geometry.location,
        map: map,
        title: SearchLocation.formatted_address,
        icon:{
        url: "http://maps.google.com/mapfiles/kml/pal2/icon13.png"
        }
        });

        google.maps.event.addListener( searchMarker, 'click', function () {
        infowindow.open(map, searchMarker);
        });
        }

        TrialLocations.forEach(function(TrialLocation){
        console.log( TrialLocation.Address );
        getTrialLocationGeoCode(TrialLocation, function( pGeo, pLocation ){
        if( pGeo !== false && typeof pGeo != 'undefined') {
        var TrialLocationMarker = new google.maps.Marker({
        position: pGeo.geometry.location,
        map: map,
        title: pLocation.Name
        });
        google.maps.event.addListener(TrialLocationMarker, 'click', function (TrialLocation, i) {
        return function () {
        var infowindow = new google.maps.InfoWindow(
        {
        content: '<b>' + pLocation.Name + '</b>',
        size: new google.maps.Size(150, 50)
        });
        infowindow.open(map, TrialLocationMarker);
        }
        });
        }
        });


        });
        }

        function getTrialLocationGeoCode( pLocation, callback )
        {
        console.log ("init trial geo");
        gGeoCoder = new google.maps.Geocoder();
        if ( gGeoCoder ) {
        gGeoCoder.geocode({
        'address': pLocation.Address
        }, function (results, status)
        {
        console.log("inside the function for location geo");
        if ( status === google.maps.GeocoderStatus.OK ) {
        if ( status !== google.maps.GeocoderStatus.ZERO_RESULTS ) {
        console.log("inside trialgeo" + results[0] );
        callback( results[0] , pLocation);
        } else {
        console.log("error 1");
        return false;
        }
        } else {
        console.log("error 2");
        return false;
        }
        });
        } else {
        console.log("error 3");
        return false;
        }
        }





        function getMap( NewMap, SearchLocation = false ) {
        var i = 0;
        var map = new google.maps.Map(document.getElementById(MapID), NewMap);
        var TrialLocationAddress;

        if( SearchLocation !== false )
        {
        var infowindow = new google.maps.InfoWindow(
        {
        content: '<b>Your Location:</b> ' + SearchLocation.formatted_address,
        size: new google.maps.Size(150, 50)
        });

        var searchMarker = new google.maps.Marker({
        position: SearchLocation.geometry.location,
        map: map,
        title: SearchLocation.formatted_address,
        icon:{
        url: "http://maps.google.com/mapfiles/kml/pal2/icon13.png"
        }
        });

        google.maps.event.addListener( searchMarker, 'click', function () {
        infowindow.open(map, searchMarker);
        });
        }

        TrialLocations.forEach(function(TrialLocation){
        console.log( TrialLocation.Address );
        getTrialLocationGeoCode(TrialLocation, function( pGeo, pLocation ){
        if( pGeo !== false && typeof pGeo != 'undefined') {
        var TrialLocationMarker = new google.maps.Marker({
        position: pGeo.geometry.location,
        map: map,
        title: pLocation.Name
        });
        google.maps.event.addListener(TrialLocationMarker, 'click', function (TrialLocation, i) {
        return function () {
        var infowindow = new google.maps.InfoWindow(
        {
        content: '<b>' + pLocation.Name + '</b>',
        size: new google.maps.Size(150, 50)
        });
        infowindow.open(map, TrialLocationMarker);
        }
        });
        }
        });


        });
        }

        function getTrialLocationGeoCode( pLocation, callback )
        {
        console.log ("init trial geo");
        gGeoCoder = new google.maps.Geocoder();
        if ( gGeoCoder ) {
        gGeoCoder.geocode({
        'address': pLocation.Address
        }, function (results, status)
        {
        console.log("inside the function for location geo");
        if ( status === google.maps.GeocoderStatus.OK ) {
        if ( status !== google.maps.GeocoderStatus.ZERO_RESULTS ) {
        console.log("inside trialgeo" + results[0] );
        callback( results[0] , pLocation);
        } else {
        console.log("error 1");
        return false;
        }
        } else {
        console.log("error 2");
        return false;
        }
        });
        } else {
        console.log("error 3");
        return false;
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 1:28









        joebjoeb

        346218




        346218






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403717%2fjavascript-function-not-waiting-for-function-to-finish%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            SQL update select statement

            'app-layout' is not a known element: how to share Component with different Modules