initiating position.location , how to combine into a single function?

in my program, the watch calls my onPosition function, and towards the start of the program I have it "initiate" points to be used.  Is it possible to compress this code into a single function, instead of having to repeat the calls three times? 

class ProgramView extends WatchUi.View {
    var pointA, pointB, pointC; 
	
    function initiatePoints (minfo) { 
		if ( (minfo != null) && (minfo.accuracy != null) ) { 
			var curpos = minfo.position.toDegrees();  
			
			if (pointA == null) {
				pointA = new Position.Location({ :latitude => curpos[0], :longitude => curpos[1], :format => :degrees });
			} else {
				pointA.initialize({ :latitude => curpos[0], :longitude => curpos[1], :format => :degrees });		
			}
			
			if (pointB == null) {
				pointB = new Position.Location({ :latitude => curpos[0], :longitude => curpos[1], :format => :degrees });
			} else {
				pointB.initialize({ :latitude => curpos[0], :longitude => curpos[1], :format => :degrees });		
			}
			
			if (pointC == null) {
				pointC = new Position.Location({ :latitude => curpos[0], :longitude => curpos[1], :format => :degrees });
			} else {
				pointC.initialize({ :latitude => curpos[0], :longitude => curpos[1], :format => :degrees });		
			}
			//...

 

I tried to create a smaller function like the below, however it isn't working as expected.  The point data is somehow erroneously duplicating, as if it is only defining/redefining a single object.  When I load the memory view in the simulator, when I go to print out the different pointA, PointB, PointC variables, they all end up referring to the same object, the same object number.  Thus, it appears as if my attempt to do this, was unsuccessful.  

// pardon this code has not been recently tested, I'm typing from memory

class ProgramView extends WatchUi.View {
    var pointA, pointB, pointC; 
	
	function initiatePoints (minfo) { 
		if ( (minfo != null) && (minfo.accuracy != null) ) {
			var curpos = minfo.position.toDegrees();  
			
			setPoint(pointA, curpos);
			setPoint(pointB, curpos);
			setPoint(pointC, curpos);
		//...
	}
	
	function setPoint(locationVar, posInDegrees)
		if (locationVar == null) {
			locationVar = new Position.Location({ :latitude => posInDegrees[0], :longitude => posInDegrees[1], :format => :degrees });
		} else {
			locationVar.initialize({ :latitude => posInDegrees[0], :longitude => posInDegrees[1], :format => :degrees });		
		}
	}

I think in order to stick the initiating code into a separate function, I have to use reference values or somehow specify that I am initiating separate variable objects, not the same object.  Can anyone give me some good clues on how to do this, so I can prevent unnecessary duplication of code?

(apologies: the second code segment I wrote from memory, because I had to remove that functionality because it didn't work.  I may have tried to use reference variables or somehow re-write the "setPoint" function so that it was better than how it appears here, however I was unable to make it work.)

  • Could you please tell me what the preferred method of changing the position data is? Do you mean using Position.parse?

    What is wrong with assigning to the variable...

    pointA = new Position.Location({
      :latitude => curpos[0],
      :longitude => curpos[1],
      :format => :degrees
    });

  • thanks.  What would your preferred technique be for changing a point, such as changing the member variables inside it... would you do pointA.latitude = newLatitude; pointA.longitude = newLongitude; ?  thanks

  • Probably just

    pointA = new Position.Location({
      :latitude => newLatitude,
      :longitude => newLongitude,
      :format => :degrees
    });

    would do it.

  • Thanks. What if I wanted to change/edit a point from one physical location to another several times during an activity?  

    The "new" operator will give me a new position object. I suppose I would just null out the old object.  But I change the object several times over the course of an activity, maybe as many as ten to twenty times, depending upon the task. Wouldn't it save memory to just stick with the same object, and adjust it's member variables, as opposed to nulling and reallocating new objects?  Thanks in advance for your feedback

  • When you do the new, the first  pointA is freed up, so no increase in memory  (the reference count to the initial object is 0 - same as if you did pointA=null;)

  • You aren't supposed to be able to mutate the Position.Location.

    The only reason you'd need to modify the original object would be if you had code in different parts of your application referencing that object and you wanted them to all be updated when you changed the object. In that case, you could either hold on to the lat/lon values in a class of your own, or you could allow parts of your application to hold on to an object that holds the Location objects. That way when you replace a Location everyone gets updated.

    class PointHolder
    {
        public pointA;
        public pointB;
        public pointC;
    }
    
    class ProgramView extends WatchUi.View
    {
        var pointHolder;
    
        function initialize() {
            View.initialize();
    
            pointHolder = new PointHolder();
        }
        
        function initializePoints(minfo) {
            if ( (minfo != null) && (minfo.accuracy != null) ) { 
                var curpos = minfo.position.toDegrees();  
                
                holder.pointA = new Position.Location({ :latitude => curpos[0], :longitude => curpos[1], :format => :degrees });
                holder.pointB = new Position.Location({ :latitude => curpos[0], :longitude => curpos[1], :format => :degrees });
                holder.pointC = new Position.Location({ :latitude => curpos[0], :longitude => curpos[1], :format => :degrees });
            }
            // ...
        }
    }