从离线应用程序获取位置

时间:2013-11-22 16:46:55

标签: android gps

我想通过GPS提供商获取Geo位置。所以我的代码就像这样

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
LocationManager lm;
TextView textLatitude, textLongitude;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_main);

    textLatitude = (TextView)findViewById(R.id.textLatitude);
    textLongitude = (TextView)findViewById(R.id.textLongitude);

    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

   /* Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);*/
}

public void onResume() {
    super.onResume();
    setup();
}

public void onStart() {
    super.onStart();
    boolean gpsEnabled, networkEnabled;
    gpsEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    Log.d("LocationService","GPS Status: "+gpsEnabled);


    if(!gpsEnabled) {
        networkEnabled = 
                lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if(!networkEnabled) {
            Intent intent = 
                    new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
    }
}

public void onStop() {
    super.onStop();
    lm.removeUpdates(listener);
}

public void setup() {
    lm.removeUpdates(listener);
    String latitude = "Unknown";
    String longitude = "Unknown";



    Location gpsLocation = requestUpdatesFromProvider(
            LocationManager.GPS_PROVIDER, "GPS not supported");

    if(gpsLocation != null) {
        Log.d("Location Service","GET FROM GPS");
        latitude = String.format("%.7f", gpsLocation.getLatitude());
        longitude = String.format("%.7f", gpsLocation.getLongitude());
    }

    textLatitude.setText(latitude);
    textLongitude.setText(longitude);
}

public Location requestUpdatesFromProvider(final String provider
        , String error) {
    Location location = null;
    if (lm.isProviderEnabled(provider)) {

        lm.requestLocationUpdates(provider, 0, 0, listener);
        Log.d("LocationService","Requesting...");

        location = lm.getLastKnownLocation(provider);

       //Log.d("LocationSerivce", "Geo :"+location.getLatitude());
    } else {
        Toast.makeText(this, error, Toast.LENGTH_LONG).show();
    }
    return location;
}

public  LocationListener listener = new LocationListener() {
    public void onLocationChanged(Location location) {
        textLatitude.setText(String.format("%.7f", location.getLatitude()));
        textLongitude.setText(String.format("%.7f",location.getLongitude()));
    }

    public void onProviderDisabled(String provider) { }

    public void onProviderEnabled(String provider) { }

    public void onStatusChanged(String provider
            , int status, Bundle extras) { }
};
}

结果是......它不起作用。我做错了什么? 我已经添加了清单文件的权限。

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.locationservice"
    android:versionCode="1"
    android:versionName="1.0" >



<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name
        ="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.locationservice.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>

我从互联网上搜索了很多,如果我与网络提供商合并,我必须使用互联网来获取我的位置。这不是我想要的。

我想从GPS获取位置只是因为我将用于离线应用程序。 你能给我一些很好的建议吗?我会非常感谢

0 个答案:

没有答案