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!

mkyong
  • 12,497
  • 12
  • 37
  • 56

3 Answers3

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); 
    }           
};
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
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

Community
  • 1
  • 1
pete
  • 24,141
  • 4
  • 37
  • 51
  • 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? – mkyong Jan 31 '12 at 15:58
  • 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. – pete Jan 31 '12 at 16:11
0

instead of

if (loc_marker != null) {

try

if (typeof loc_marker != 'undefined') {
André Alçada Padez
  • 10,987
  • 24
  • 67
  • 120