When I use Geocoding APIs on my code, I assigned var latitude as an global(line 20), and in function geocoder, variable works. But in line 35, it throws out error as 0. Even though it works in function scope.
14 <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
15 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
16 <script type="text/javascript">
17
18 var geocoder = new google.maps.Geocoder();
19 var longitude = 0.0;
20 var latitude = 0.0;
21
22 if (geocoder) {
23 geocoder.geocode({ 'address': 'Louvre Museum' }, function (results, status) {
24 if (status == google.maps.GeocoderStatus.OK) {
25 longitude = results[0].geometry.location.nb;
26 latitude = results[0].geometry.location.ob;
27 console.log(results[0].geometry.location);
28 }
29 else {
30 console.log('No results found: ' + status);
31 }
32 });
33 }
34
35 console.log(latitude);