javascript - callback function for Google Geocode API not executing immediately -
I see this behavior when I move through this code: Response handler code is skipped when The rest of the function is finished, and then executes the handler code. It's definitely not what I want, because the code that comes after the response depends on the code in the response handle.
var geocoder = new google.maps.Geocoder (); Functional Initialize Place () {var Destination_Letleng; Var destination = document.getElementById ("destination_address"). value; Geocoder.geocode ({'address': destination}, function (result, status) {if (position == google.maps.GeocoderStatus.OK) {destination_LatLng = results [0] .geometry.location;} and if (position = = Google.maps.GeocoderStatus.ZERO_RESULTS) {Warning ("Bad Destination Address.");} And {Warning ("Error calling Google Geocode API.");}});
What is the reason for this behavior, and to ensure that I run the callback before the code given below, how can I change my code?
Geocode runs with asynchronous, so you have to keep that code inside the callback, or another callback Function must be created:
geocoder.geocode ({'address': destination}, function (result, status) {if (status == google.maps.GeocoderStatus.OK) {destination_LatLng = results [0] .geometry.location;} and if (condition == google.maps.GeocoderStatus.ZERO_RESULTS) {warnings ("bad destination address.");} And {warnings ("error extinguishing Google Geocode API.") }} // Keep more stuff here instead}};
or
Function more stuff () {// more stuff here} geocoder.geocode ({'address': destination}, function (result, status) {If (status == google.maps.GeocoderStatus.OK) {destination_LatLng = results [0 ] .geometry.location;} and if (position == google.maps.GeocoderStatus.ZERO_RESULTS) {warnings ("bad destination address.");} And {warnings ("Google Geocode API called error.");} More Stuff ();});
Comments
Post a Comment