There is such a post

jQuery.post("/gs/mod/modules/map_coord_city.php",{ city_name : city_n }, function(data){ CurrentLoc=''; CurrentLoc=data; var Location_coords = CurrentLoc.split(','); latitude=Location_coords[0]; longitude=Location_coords[1]; } ); 

I need to use Longitute and latitude outside of this function. How to share them?

  • You can assign them to the global window element, but this is a minus in karma, so it may be worth revisiting the architecture. Why and when do you need to use these values? - ikoolik
  • I use them in different functions. Basically, to go to the city, by the coordinates that I got, i.e. by these values. Functions are not called immediately after the search for coordinates. mainly by clicking on certain buttons and other form elements - LIVE
  • perhaps it is necessary to operate with some object for the city? The context is still not clear. Is work with several cities going on at the same time or is there one active city and is information about it loaded by this very post? - ikoolik
  • only one of several cities is active, when the city changes, info about it is loaded by this post - ZhUZHU
  • perhaps it is worth creating an ActiveCity object in the global scope with fields name, latitude, longitude and what else is there? In the host function, the server’s response will have to replace latitude and longitude with ActiveCity.latitude and ActiveCity.longitude. A still rough option, but better. - ikoolik

2 answers 2

Comments are over = (

  1. 76b9 third character - the letter. Is this a server response or a typo?
  2. Add some button to display the current value of currentLocation.lat and double-check after running ajax
  3. The "card responds" context to the studio if it matters
  • 1. Typo 76.9 3. in general, depending on the coordinates received, the city should be displayed. The place is shown solely by the initial values, but it must be found by the results - LIVE
  • replace the comma with a period - ikoolik
  • Sorry. in the data it is: 76.9 - I 'm
  • as I understand, the data is obtained correctly with ajax and now the problem is that the card does not respond. Need context =) - ikoolik
  • I am confused by two things: 1. What if this post is removed and initial values ​​are given, the map will indicate by initial values ​​2. And that “0” is output after the function body. - LIVE

For example:

 var latitude; var longitude; jQuery.post("/gs/mod/modules/map_coord_city.php",{ city_name : city_n }, function(data){ CurrentLoc=''; CurrentLoc=data; var Location_coords = CurrentLoc.split(','); latitude=Location_coords[0]; longitude=Location_coords[1]; } ); function test() { alert(latitude); } 
  • "global variables are evil" (c) - ikoolik
  • In response, received: [object HTMLInputElement] - I LIVE
  • try outputting it in console.log () and see what's there. The result is some unexpected) - ikoolik
  • @ikoolik, evil, yes, but maybe this is a sample code from a closure? - neoascetic
  • in the sense of "closure"? - I 'll live