2

I don't think this is possible from what I read, but is there away to check if my map marker is null in the following code? I read something about prototypes, but I'm not sure if that's what I need to use. Here is the code:

this.currentLocation = function(latitude, longitude) {
    if (loc_marker != null) {
        loc_marker.setPosition(new google.maps.LatLng(latitude, longitude);
    }
    else {
        var loc_marker = new google.maps.Marker({
            position: new google.maps.LatLng(latitude, longitude),
            title: "Here"
        });
        loc_marker.setMap(map); 
    }           
};

I'm trying to move a marker on a Google Map each time the location changes. I want to check to see if a marker has been set, and if it has then I'll just change the position using setPosition();

If you have any suggestions I'd really appreciate it!

3 Answers 3

2

All of the following will do what you're looking for...

if (loc_marker != null) {  //Tests for null and for undefined
if (loc_marker === null) { //Tests for null
if (typeof loc_marker === 'object' && loc_marker === null) {  //Another null test, albeit redundant
if (typeof loc_marker === 'undefined') {  //Tests for undefined
if (loc_marker) { //Test for a defined non-null value

That said, where is loc_marker actually defined? It looks "like" you're trying to define it in the else block (although JavaScript doesn't work that way).

Save for where you're declaring loc_marker, the code you have should work fine.

Hope this helps,

Pete

Sign up to request clarification or add additional context in comments.

2 Comments

Where the marker is defined seems to be my biggest problem. I'm not sure where to declare it. If I do it outside of the expression, a new marker will be created each time. I need to just make one marker and then change it's position each time the location changes. I think you're close to getting this worked out. Any suggestions on how/where to declare it?
If you only need one marker, I would make it part of this, so in the same scope as this.currentLocation, add this.loc_marker = {};. That way you can reference this.loc_marker instead of just loc_marker in your function.
2

You will have to make loc_marker a "global" variable, i.e. that the variable points to the same object each time the function is called. In your code above, you only will create another local variable in the scope of the function. Try something like:

var loc_marker = null;
this.currentLocation = function(latitude, longitude) {
    if (loc_marker != null) {
        loc_marker.setPosition(new google.maps.LatLng(latitude, longitude);
    }
    else {
        loc_marker = new google.maps.Marker({
            position: new google.maps.LatLng(latitude, longitude),
            title: "Here"
        });
        loc_marker.setMap(map); 
    }           
};

Comments

0

instead of

if (loc_marker != null) {

try

if (typeof loc_marker != 'undefined') {

Comments

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.