1

It's pretty hard to explain so I'll just show an example.

for(i=1;i<position.length;i++)
        {   
            if(position[i].lat() == position[i-1].lat() && position[i].lng() == position[i-1].lng())
            {

                position[i].lat = position[i].lat() + ((Math.random() -.5) / 1500);
                position[i].lng = position[i].lng() + ((Math.random() -.5) / 1500);
                //position[i].lng = position[i].lng() + ((Math.random() -.5) / 1500);
                //console.log(position[i].lat().value);
            }
        }

Basically I'm trying to just change the value of lat and lng in the position array but I can't seem to find a solution.

 $('#data').find('div[name=postalCode]').each(function(i) {
            var latlng = $(this).find('input[type=hidden]');
            position.push(new google.maps.LatLng(latlng[0].value, latlng[1].value));
            });

And this is how the position array is filled up.

Any help is appreciated. thanks Realize that the above comparison code does not fully compare, i'll put the new code here just in case anyone uses it

for (var i = 0; i < position.length; i++) {
            for(var k = i+1; k<position.length; k++){
        if (position[i].lat() == position[k].lat() && (position[i].lng() == position[k].lng())) {
            console.log("before change" + i + position[i].lat());
           position[i] = new google.maps.LatLng(
        position[i].lat() + ((Math.random() * 1.5) / 100),
        position[i].lng() + ((Math.random() * 1.5) / 100)
        );
        console.log("after change" +i + position[i].lat());
        }
        }
    }
6
  • Is lat and lng a property or function on your array? You're calling them both ways. Commented Mar 30, 2017 at 17:12
  • What about removing () at the end of lat and lng. Commented Mar 30, 2017 at 17:13
  • Hi, I edited the post to show how position[] gets its data. Commented Mar 30, 2017 at 17:29
  • still. lat() means you are calling a function. but it is not a function. Remove the () Commented Mar 30, 2017 at 17:30
  • 1
    Is position array of google map markers? or just json object? Commented Mar 30, 2017 at 17:37

1 Answer 1

1

https://developers.google.com/maps/documentation/javascript/reference

"Notice that you cannot modify the coordinates of a LatLng. If you want to compute another point, you have to create a new one."

In short, don't change the values, replace the entry.

Example:

for (var i = 1; i < position.length; i++) {
    if (position[i].lat() == position[i - 1].lat() && position[i].lng() == position[i - 1].lng()) {
        position[i] = new google.maps.LatLng(
            position[i].lat() + ((Math.random() - .5) / 1500),
            position[i].lng() + ((Math.random() - .5) / 1500)
        );
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the help, didn't know that i was just going in a wrong direction the whole time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.