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

Android™ Runtime Development

Reply
New Contributor
sgao
Posts: 9
Registered: ‎07-31-2013
My Device: developer

Can not get Longitude & Latitude

I'm porting android app to BB10. I used folowing code to get Longitude&Latitude. It works fine on android devices, but it can't get  Longitude&Latitude in Z10. 

 

Please tell me how to get  Longitude&Latitude in BB10. 

 

thanks.

 

Code:

// code to get Longitude&Latitude

GpsLocationTracker mGpsLocationTracker = new GpsLocationTracker(LocAct.this);
/**
* Set GPS Location fetched address
*/
if (mGpsLocationTracker.canGetLocation())
{
latitude = mGpsLocationTracker.getLatitude();
longitude = mGpsLocationTracker.getLongitude();

}

 

 

 

//code of GpsLocationTracker class:

 

public class GpsLocationTracker extends Service implements LocationListener
{

/**
* context of calling class
*/
private Context mContext;

/**
* flag for gps status
*/
private boolean isGpsEnabled = false;

/**
* flag for network status
*/
private boolean isNetworkEnabled = false;

/**
* flag for gps
*/
private boolean canGetLocation = false;

/**
* location
*/
private Location mLocation;
private double mLatitude;
private double mLongitude;
private double mRadius;
double mGpsLat,mGpsLong,mGpsRadius,mNetLat,mNetLong,mNetRadius;

/**
* min distance change to get location update
*/
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATE = 10;

/**
* min time for location update
* 60000 = 1min
*/
private static final long MIN_TIME_FOR_UPDATE = 60000;

/**
* location manager
*/
private LocationManager mLocationManager;

//Timer timer1;
/**
* @param mContext constructor of the class
*/
public GpsLocationTracker(Context mContext) {

this.mContext = mContext;
getLocation();
}


/**
* @return location
*/
public Location getLocation() {

try {

mLocationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

/*getting status of the gps*/
isGpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
/*getting status of network provider*/
isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if (!isGpsEnabled && !isNetworkEnabled) {

/*no location provider enabled*/
} else {

this.canGetLocation = true;
/*getting location from network provider*/
if (isNetworkEnabled) {

mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_FOR_UPDATE, MIN_DISTANCE_CHANGE_FOR_UPDATE, this);

if (mLocationManager != null) {

mLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if (mLocation != null) {

mNetLat = mLocation.getLatitude();
mNetRadius=mLocation.getAccuracy();
mNetLong = mLocation.getLongitude();
}
}
}
/*if gps is enabled then get location using gps*/
if (isGpsEnabled) {

if (mLocation == null) {

mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_FOR_UPDATE, MIN_DISTANCE_CHANGE_FOR_UPDATE, this);
if (mLocationManager != null) {

mLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if (mLocation != null) {

mGpsLat = mLocation.getLatitude();
mGpsRadius=mLocation.getAccuracy();
mGpsLong = mLocation.getLongitude();
}

}
}

}



}
} catch (Exception e) {
e.printStackTrace();
}
return mLocation;
}
/**
* call this function to stop using gps in your application
*/
public void stopUsingGps() {

if (mLocationManager != null) {

mLocationManager.removeUpdates(GpsLocationTracker.this);

}
}

/**
* @return latitude
* <p/>
* function to get latitude
*/
public double getLatitude() {

if (mLocation != null) {

//比较GPS和Network取得的经纬
if (mGpsRadius<=mNetRadius)
{
mLatitude=mGpsLat;
}
else
{
mLatitude=mNetLat;
}
}
return mLatitude;
}

/**
* @return longitude
* function to get longitude
*/
public double getLongitude() {

if (mLocation != null) {

//比较GPS和Network取得的经纬
if (mGpsRadius<=mNetRadius)
{
mLongitude=mGpsLong;
}
else
{
mLongitude=mNetLong;
}

}

return mLongitude;
}

public double getRadius() {

if (mLocation != null) {

//比较GPS和Network取得的经纬
if (mGpsRadius<=mNetRadius)
{
mRadius=mGpsRadius;
}
else
{
mRadius=mNetRadius;
}

}

return mRadius;
}

/**
* @return to check gps or wifi is enabled or not
*/
public boolean canGetLocation() {

return this.canGetLocation;
}

}

 

Please use plain text.
BlackBerry Development Advisor (Retired)
jjasmann
Posts: 264
Registered: ‎01-08-2013
My Device: BlackBerry 10 Dev Alpha B

Re: Can not get Longitude & Latitude

Hey sgao,

 

This is a lot of code to go through.Would you be able to debug and isolate the area that you're experiencing problems with?

 

If you can't get longitude/latitude, what do you get?

 

Also, what OS version are you running?

 

    Justin

Justin Jasmann | Android Application Development Consultant
Developer Relations
developer.blackberry.com
Please use plain text.
Contributor
varon_juan
Posts: 10
Registered: ‎12-12-2011
My Device: Developer- 9800, 9780, 9300, 9700, 9105, 9900, 9360

Re: Can not get Longitude & Latitude

Hi sgao,

 

which OS version are you trying this on??? I  don't know if it will be helpful or not, but searching for a celllocation related issue I have, I found a question on stacktrace where it says that 

LocationManager.getLastKnownLocation (....)

was always returning null. Is this your case?

the issue was in the android runtime environment (or so it says) an it was fixed in OS 10.1.

 

Here's the link, hopefully  it will help you out


http://stackoverflow.com/questions/15370467/cannot-get-network-location-from-bb10-device

 

cheers!!

Please use plain text.