PDA

View Full Version : مشکل در دریافت جی پی اس



mahdiaspx
پنج شنبه 24 دی 1394, 17:03 عصر
با سلام خدمت دوستان
من یک برنامه مکان یاب جی پی اسی نوشتم که توش یه مشکلی برام پیش اومده . درست کار میکنه اما گاهی اوقات که نمیتونه نقطه جی چی اس رو بگیره برنامه فورس کلوز میده با try catch هم درست نشد که بتونم خطاش رو کنترل کنم . لطفا راهنمایی کنید . ممنون

من یک کلاس دارم به اسم MyLocation.java

package com.example.samen;


import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class MyLocation {

Timer timer1;
LocationManager lm;
LocationResult locationResult;
boolean gps_enabled=false;
boolean network_enabled=false;

public boolean getLocation(Context context, LocationResult result)
{
//I use LocationResult callback class to pass location value from MyLocation to user code.
locationResult=result;
if(lm==null)
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE) ;

//exceptions will be thrown if provider is not permitted.
try{gps_enabled=lm.isProviderEnabled(LocationManag er.GPS_PROVIDER);}catch(Except​ion ex){}
try{network_enabled=lm.isProviderEnabled(LocationM anager.NETWORK_PROVIDER);}catc​h(Exception ex){}

//don\'t start listeners if no provider is enabled
if(!gps_enabled && !network_enabled)
return false;

if(gps_enabled)
lm.requestLocationUpdates(LocationManager.GPS_PROV IDER, 0, 0, locationListenerGps);
if(network_enabled)
lm.requestLocationUpdates(LocationManager.NETWORK_ PROVIDER, 0, 0, locationListenerNetwork);
timer1=new Timer();
timer1.schedule(new GetLastLocation(), 20000);
return true;
}

LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerNetwork);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};

LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};

class GetLastLocation extends TimerTask {
@Override
public void run() {
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);

Location net_loc=null, gps_loc=null;
if(gps_enabled)
gps_loc=lm.getLastKnownLocation(LocationManager.GP S_PROVIDER);
if(network_enabled)
net_loc=lm.getLastKnownLocation(LocationManager.NE TWORK_PROVIDER);

//if there are both values use the latest one
if(gps_loc!=null && net_loc!=null){
if(gps_loc.getTime()>net_loc.getTime())
locationResult.gotLocation(gps_loc);
else
locationResult.gotLocation(net_loc);
return;
}

if(gps_loc!=null){
locationResult.gotLocation(gps_loc);
return;
}
if(net_loc!=null){
locationResult.gotLocation(net_loc);
return;
}
locationResult.gotLocation(null);
}
}

public static abstract class LocationResult{
public abstract void gotLocation(Location location);
}

}




که اینطوری تو OnCreate یه اکتیویتی صداش میکنم

try
{
LocationResult locationResult = new LocationResult(){
@Override
public void gotLocation(Location location){
FindNearBranchMethodDirect(String.valueOf(location .getLatitude()),String.valueOf​(location.getLong itude()));
}
};
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
}
catch (Exception e) {
// TODO: handle exception
txtNearBranch.setText("خطا در دریافت موقعیت فعلی شما"+"\n"+"لطفا با بازگشت به منوی اصلی و برگشتن به این صفحه دوباره سعی کنید");
}





اینم متدی که صدا میزنم برای پیدا کردن نزدیکترین شعبه

public void FindNearBranchMethodDirect(String MyLatC,String MyLngC)
{
float Res = 0,FinalRes = 0;

String MyLat = MyLatC;
String MyLng = MyLngC;

db = dbh.getReadableDatabase();
Cursor cursors = db.rawQuery("select ID,BranchCode,BranchName,BranchTel,BranchAddress,l at,lng from Branchs order by BranchName", null);

String Branchlng= null,BranchLat= null,BranchId= null,BranchName = null,BranchAddress= null,BranchTel= null,BranchCode= null;

if(cursors.getCount() > 0)
{

for (int i = 0; i < cursors.getCount(); i++) {
cursors.moveToNext();
BranchLat = cursors.getString(cursors.getColumnIndex("lat"));
Branchlng = cursors.getString(cursors.getColumnIndex("lng"));

Res = distFrom(Float.valueOf(MyLat), Float.valueOf(MyLng), Float.valueOf(BranchLat), Float.valueOf(Branchlng));
if(i==0)
{
FinalRes = Res;
}
if(Res < FinalRes)
{
FinalRes = Res;
BranchId = cursors.getString(cursors.getColumnIndex("ID"));
BranchName = cursors.getString(cursors.getColumnIndex("BranchName"));
BranchAddress=cursors.getString(cursors.getColumnI ndex("BranchAddress"));
BranchTel=cursors.getString(cursors.getColumnIndex ("BranchTel"));
BranchCode = cursors.getString(cursors.getColumnIndex("BranchCode"));
}
}

txtNearBranch.setText("نزدیکترین شعبه به شما : "+"\n"+"شعبه "+BranchName+"\n"+"کد شعبه : "+BranchCode+"\n"+"تلفن : "+BranchTel+"\n"+"آدرس : "+BranchAddress);

}
}




اینم متدا محاسبه فاصله دو نقطه جی پی اس

public static float distFrom(float lat1, float lng1, float lat2, float lng2) {
double earthRadius = 6371000; //meters
double dLat = Math.toRadians(lat2-lat1);
double dLng = Math.toRadians(lng2-lng1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
float dist = (float) (earthRadius * c);

return dist;
}




همه چی درسته ... فقط وقتی جی پی اس جواب نده و نقطه نگیره برنامه فورس کلوز میده میاد بیرون
لطفا راهنمایی کنید . ممنون