BlackBerry Forums Support Community

BlackBerry Forums Support Community (http://www.blackberryforums.com/)
-   Developer Forum (http://www.blackberryforums.com/developer-forum/)
-   -   Can't stop Coordinates from updating (http://www.blackberryforums.com/developer-forum/144506-cant-stop-coordinates-updating.html)

kmdcomp 08-10-2008 08:44 AM

Can't stop Coordinates from updating
 
Hi Everyone, I'm kind of relearning java as I go along here, so please excuse any unconventional coding practices, but feel free to point them out to me, I havenít used java in many years :?


I wanted to play around with GPS on my blackberry 8830 from Verizon. So I bought at BT GPS puck and everything is good there.

I'm trying to modify the GPSDemo project that came with the JDE. I stripped out the stuff that I didn't need, server connection, waypoints, etc...

All I want to accomplish is for the program to show the current position as I move and the speed. This is working already and was left over from the original app.

I also want to be able to mark a point, then move a bit and mark a second point and get the distance between the 2 points. This is where I'm having trouble. The program is returning the GPS info just fine and every time it sends some data I'm storing it in Location object called currentLocation.

When I click on a menu option, it should set the first point and when I click again, it should set the second point. Instead, after I set the first point, it keeps changing when new GPS info comes in and when I set point 2, it does the same thing. Both points are constantly synchronized to the currentLocation.


Code:

//global veriables to store GPS data
    private Location currentLocation;
    private Coordinates loc1;
    private Coordinates loc2;

//called whenever gps data comes in
public void updateLoc(Location l)
    {
        currentLocation = l; 
               
       
    }

//called whenever the markPosition menu button is selected
 private void setLoc()
    {
            //if the first point has not been selected yet
            //then set loc1 to the current coordinates and set
          //pointMarked to true
            if(pointMarked == false)
            {
                pointMarked = true;
                loc1 = currentLocation.getQualifiedCoordinates();
               
               
            }
            //Otherwise, set loc2 to current and set pointmarked to false
            else
            {
                pointMarked = false;
                loc2 = currentLocation.getQualifiedCoordinates();
                             
                distance = loc2.distance(loc1);
               
            }
    }






//Impementation of the locationListner class
private class LocationListenerImpl implements LocationListener
    {
        // Members ----------------------------------------------------------------------------------------------
        private int captureCount;
        private int sendCount;
       
        // Methods ----------------------------------------------------------------------------------------------
        /**
        * @see javax.microedition.location.LocationListener#locationUpdated(LocationProvider,Location)
        */
        public void locationUpdated(LocationProvider provider, Location location)
        {
            if(location.isValid())
            {
                float heading = location.getCourse();
                double longitude = location.getQualifiedCoordinates().getLongitude();
                double latitude = location.getQualifiedCoordinates().getLatitude();
                float altitude = location.getQualifiedCoordinates().getAltitude();
                float speed = location.getSpeed();               
             
              // Information to be displayed on the device.
                StringBuffer sb = new StringBuffer();
                sb.append("Longitude: ");
                sb.append(longitude);
                sb.append("\n");
                sb.append("Latitude: ");
                sb.append(latitude);
                sb.append("\n");
                sb.append("Altitude: ");
                sb.append(altitude);
                sb.append(" m");
                sb.append("\n");
                sb.append("Heading relative to true north: ");
                sb.append(heading);
                sb.append("\n");
                sb.append("Speed : ");
                sb.append(speed);
                sb.append(" m/s");
                sb.append("\n");
                sb.append("Grade : ");
             
                GPSDemo.this.updateLocationScreen(sb.toString());
               
                //call method to update the global currentLocation
                GPSDemo.this.updateLoc(location);
            }
        }
 
        public void providerStateChanged(LocationProvider provider, int newState)
        {
            // Not implemented.
        }       
    }


hrbuckley 08-10-2008 10:53 AM

Made the same mistake when I started. The Location API is giving you a reference to an internal object. You need to make a new one to store the values:

loc1 = new QualifiedCoordinates(currentLocation.getQualifiedC oordinates().getLatitude(), ...);

Tedious but it works. If someone has a better way I would like to see it as well.

kmdcomp 08-10-2008 11:21 AM

Thanks hrbuckley, I figured it had to be something like that. I'll give it a try, but I suspect it's going to work.


All times are GMT -5. The time now is 01:44 PM.

Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.