aktuelle latitude & Längengrad in android-fragment-Klasse

Wie bekomme ich die Aktuelle Lage.ich habe versucht folgenden code, aber immer null für Standort-

public class LocationsMapView extends Fragment implements LocationListener, android.location.LocationListener{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

  locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,LocationsMapView.this);
    //Define the criteria how to select the locatioin provider -> use
    //default
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    location = locationManager.getLastKnownLocation(provider);

    //Initialize the location fields
    if (location != null) {
      System.out.println("Provider " + provider + " has been selected.");
      onLocationChanged(location);
    } else {
      System.out.println("Location not available");

    }
}

@Override
    public void onLocationChanged(Location loc) {
        current=new LatLng(loc.getLatitude(),loc.getLongitude());
        lat=loc.getLatitude();longi=loc.getLongitude();
        Toast.makeText(getActivity(), "location-"+loc.getLatitude(), Toast.LENGTH_SHORT).show();
        System.out.println("location.."+lat+"..."+longi+" current .."+current);
    }

    @Override
    public void onProviderDisabled(String arg0) {
        //TODO Auto-generated method stub
    }

    @Override
    public void onProviderEnabled(String arg0) {
        //TODO Auto-generated method stub
    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        //TODO Auto-generated method stub
    }
@Override
    public void onResume() {
        //TODO Auto-generated method stub
        super.onResume();
        location = locationManager.getLastKnownLocation(provider);

            //Initialize the location fields
            if (location != null) {
              System.out.println("Provider " + provider + " has been selected.");
              onLocationChanged(location);
            } else {
              System.out.println("Location not available"); 
            }       
}
}

Habe ich auch die Erlaubnis gegeben, in manifest-

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Versuchen Sie, diese mit GPS_PROVIDER
Haben Sie die internet-Berechtigung menifest Datei <uses-permission android:name="android.permission.INTERNET" />
ja..
habe auch versucht mit GPS_PROVIDER
Stellen Sie sicher, dass die Ortungsdienste sind aktiviert in den Einstellungen und auch schalten Sie wifi, wenn Sie haben.

InformationsquelleAutor yuva ツ | 2014-02-04

Schreibe einen Kommentar