user3962692
user3962692

Reputation:

How to store coordinates into different variables??

I use getBounds() in my google map script code to obtain latitude & longitude bounds of current viewport. I use getSouthWest() & getNorthEast() to obtain the position of south-west & north-east bounds of this map.

After adding the google map,I gave the following code inside the Javascript tags.

var bounds = map.getBounds();
var southWest = bounds.getSouthWest();
var northEast = bounds.getNorthEast();

When I used alert() to display both of the variables,I successfully obtained the bounds.

alert(southWest) gives SouthWest: (-2.871967531323266, 57.88463437500002) as output.

alert(northEast) gives NorthEast: (18.75527829568889, 97.43541562500002) as output.

I'm singing hallelujah on fourth of July. :) :) :P

Now I am trying to store these values into 4 different variables.

I want to store -2.871967531323266 into variable w, 57.88463437500002 into variable x, 18.75527829568889 into variable y, 97.43541562500002 into variable z.

Is the output that I got an array??? How can I do this??

Upvotes: 1

Views: 1106

Answers (1)

James G.
James G.

Reputation: 2904

You can use an object:

var Obj = {
'w' : -2.871967531323266,
'x' : 57.88463437500002,
'y' : 18.75527829568889,
'z' : 97.43541562500002
};

Obj['w']; //-2.871967531323266
Obj.w; //-2.871967531323266

Objects can also be declared one key at a time:

var Obj = {};

Obj['w'] = -2.871967531323266;
Obj.x = -57.88463437500002;

edit: You should study objects more closely, they're fundamental to Javascript. It is returning an object. You should also use console.log() not alert(), as it gives the full picture.

You must run the .lat() and .lng() functions on the returned object to get the latitude and longitude, as such:

  var ne = rectangle.getBounds().getNorthEast();
  var sw = rectangle.getBounds().getSouthWest();

  var contentString = 'New north-east corner: ' + ne.lat() + ', ' + ne.lng() + '<br>' +
      'New south-west corner: ' + sw.lat() + ', ' + sw.lng();

Upvotes: 3

Related Questions