-4

I want to get the current location and compute some data with latitude and longitude. My code works in some cases but doesn't work in other cases.

public class Show_gheble extends Activity implements SensorEventListener {

protected Double mylatitude,mylongitude;
boolean set_location=false;
boolean set_angel=false;
private SensorManager mSensorManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_gheble);

    image=(ImageView) findViewById(R.id.imageView1);
    image1=(ImageView) findViewById(R.id.imageView2);

    LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(context);

    String provider = LocationManager.GPS_PROVIDER;
    String provider2 = LocationManager.NETWORK_PROVIDER;

    Location location = locationManager.getLastKnownLocation(provider);
    Location location2 = locationManager.getLastKnownLocation(provider2);

    updateWithNewLocation(location,location2);

    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}

private void updateWithNewLocation(Location location,Location location2) {

String latLongString;

if (location != null) {
    mylatitude = location.getLatitude();
    mylongitude = location.getLongitude();

    makkeh_ang=get_angel(mylatitude, mylongitude);
    set_angel=true;
}
else if(location2 != null)
{
    mylatitude = location2.getLatitude();
    mylongitude = location2.getLongitude();
    makkeh_ang=get_angel(mylatitude, mylongitude);
    set_angel=true;
}
else
{
    latLongString = "مکان شما یافت نشد دوباره امتحان کنید";
    Toast.makeText(this,latLongString, Toast.LENGTH_LONG).show();
}
}
Anthon
  • 69,918
  • 32
  • 186
  • 246

1 Answers1

0

Use this latest technique to find current location. https://developer.android.com/training/location/retrieve-current.html

Dhiren
  • 160
  • 1
  • 1
  • 10