Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Trusted Contributor
Posts: 201
Registered: ‎12-17-2010
My Device: Not Specified

Get direction using latitude longitude

double next_Latitude = 21.416667;
double next_Longitude = 39.816667;

 

double current_Latitude = 19.416667;
double current_Latitude =40.816667;

 

I want to get the direction using from current_Latitude & current_Latitude  to next_Latitude & next_Longitude.

like compass.

means south,east,west,north etc..

 

if any one has any idea help me

 

Thanks

Developer
Posts: 19,629
Registered: ‎07-14-2008
My Device: Not Specified

Re: Get direction using latitude longitude

I have never tried it, but I suspect you can get the direction by creating two Cordinates objects from the two sets of points you have (altitude can be 0), and then use the azimuthTo() method to get the degrees from one coordinate to the other.

 

You should be able to convert the degree you get into a more usual direction indicator by comparing,where the result is in the range , for example 338 to 22, you could say it is North, 23 - 77 is NE, 78 - 112 is East, etc.   -

Trusted Contributor
Posts: 201
Registered: ‎12-17-2010
My Device: Not Specified

Re: Get direction using latitude longitude

Hi,

Thanks for reply.

I will try that.

 

Regards,

Trusted Contributor
Posts: 201
Registered: ‎12-17-2010
My Device: Not Specified

Re: Get direction using latitude longitude

Hi,

I tried with it.

I tried it in simulator.

I got 0.0 .

Is it correct ?

Please help me in this matter.

 

Thanks,

 

Developer
Posts: 19,629
Registered: ‎07-14-2008
My Device: Not Specified

Re: Get direction using latitude longitude

Can you create a piece of simple code, where you set the two Coordinates to specific values, then call the method and the print out the result.  If this doesn't give you the values you expect, then paste the code in here, tell us what you get and we can see if it works OK or is a coding bug. 

Trusted Contributor
Posts: 201
Registered: ‎12-17-2010
My Device: Not Specified

Re: Get direction using latitude longitude

Hi,

Here is my code :

 

package mypackage;

import javax.microedition.location.Coordinates;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;

public class GPS extends Thread {
private double latitude;
private double longitude;
private String satCountStr;
private int interval = 10;

// time in seconds to get new gps data
public GPS() {
if (currentLocation()) {
// This is going to start to try and get me some data!
}
}

private boolean currentLocation() {
boolean retval = true;
try {
LocationProvider lp = LocationProvider.getInstance(null);
if (lp != null) {
lp.setLocationListener(new LocationListenerImpl(), interval, 1,
1);
} else {
// GPS is not supported, that sucks!
// Here you may want to use UiApplication.getUiApplication() and
// post a Dialog box saying that it does not work
retval = false;
}
} catch (LocationException e) {

}
return retval;
}

private class LocationListenerImpl implements LocationListener {
public void locationUpdated(LocationProvider provider, Location location) {
if (location.isValid()) {
longitude = location.getQualifiedCoordinates().getLongitude();
latitude = location.getQualifiedCoordinates().getLatitude();

System.out.println("latitude : " + latitude);
System.out.println("longitude : " + longitude);

double prevLatitude = 21.416667;
double prevLongitude = 39.816667;

location.getQualifiedCoordinates().setLatitude(prevLatitude);
location.getQualifiedCoordinates().setLongitude(prevLongitude);
location.getQualifiedCoordinates().setAltitude(0);

Coordinates c = location.getQualifiedCoordinates();

float head = location.getQualifiedCoordinates().azimuthTo(c);

System.out.println("head : " + head);
// AddressInfo inf=new AddressInfo();
// This is to get the Number of Satellites
String NMEA_MIME = "application/X-jsr179-location-nmea";
satCountStr = location.getExtraInfo("satellites");
if (satCountStr == null) {
satCountStr = location.getExtraInfo(NMEA_MIME);
} // this is to get the accuracy of the GPS Cords
}
}

public void providerStateChanged(LocationProvider provider, int newState) {
// no-op
}
}

public double getLatitude() {
return latitude;
}

public double getLongitude() {
return longitude;
}

}

Developer
Posts: 19,629
Registered: ‎07-14-2008
My Device: Not Specified

Re: Get direction using latitude longitude

Sorry this does not appear to be what I suggested, which was

"a piece of simple code, where you set the two Coordinates to specific values, then call the method and the print out the result"

 

The trick here is the two Coordinates.  Have a close look at the code you use:

 

Coordinates c = location.getQualifiedCoordinates();

float head = location.getQualifiedCoordinates().azimuthTo(c);

 

Of course the heading is going to be 0, they are the same Coordinates!

 

I will leave you to figure out how to set a different Coordinates Object to the previous value. 

Trusted Contributor
Posts: 201
Registered: ‎12-17-2010
My Device: Not Specified

Re: Get direction using latitude longitude

Can you please give sample code that I can find the degrees between bellow ltitude & longitude ?

 

double next_Latitude = 21.416667;
double next_Longitude = -80.816667;

 

double current_Latitude = 19.416667;
double current_Latitude =-40.816667;

 

Thanks,

Developer
Posts: 19,629
Registered: ‎07-14-2008
My Device: Not Specified

Re: Get direction using latitude longitude

I hope you don't think it is too rude, but I would prefer that you created this code, as then it is a learning exercise rather than just a cut and paste job...... 

 

I have done it, it is about 7 lines, and you already have 4 of them.  But I presume that

double current_Latitude =-40.816667

was supposed to be

double current_Longitude =-40.816667;

 

I printed out the important values, here is the output.  Hope it makes sense.

 

"Go: 280.14984 to get from 19.416667,-40.816667 to 21.416667,-80.816667."

Trusted Contributor
Posts: 201
Registered: ‎12-17-2010
My Device: Not Specified

Re: Get direction using latitude longitude

Thanks for reply..

But I caould not able to understand your last answer.

please help me to get direction/degree using current city's latitude & longitude to other city's latitude longitude.

I have all values but I dont find that using those values how can I get direction/Degree.

 

Thanks,