android获取当前位置

时间:2012-01-10 10:40:48

标签: android gps

我正在测试一个使用sumsung galaxy spica获取当前位置的应用程序,它显示了另一个与我的位置不同的位置,它在AVD中显示的不一样! gps已激活,但我不知道问题究竟在哪里。 这是我的代码

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

public class MaptestActivity extends MapActivity implements LocationListener {
    private MapView mapView = null;
    private LocationManager lm = null;
    private double lat = 0;
    private double lng = 0;
    private MapController mc = null;
    private MyLocationOverlay myLocation = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mapView = (MapView) this.findViewById(R.id.mapView);
    mapView.setBuiltInZoomControls(true);

    lm = (LocationManager) this.getSystemService(LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, this);
    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, this);




    myLocation = new MyLocationOverlay(getApplicationContext(), mapView);
    myLocation.runOnFirstFix(new Runnable() {
        public void run() {
        mc.animateTo(myLocation.getMyLocation());
        mc.setZoom(17);
        mc = mapView.getController();
        lat = myLocation.getMyLocation().getLatitudeE6();
        lng = myLocation.getMyLocation().getLongitudeE6();
        Toast.makeText(
                getBaseContext(),
                "My Location : Latitude = " + lat + " Longitude = "
                    + lng, Toast.LENGTH_LONG).show();
        }
    });
    mapView.getOverlays().add(myLocation);
    }

    @Override
    protected void onResume() {
    super.onResume();
    myLocation.enableMyLocation();
    myLocation.enableCompass();
    }

    @Override
    protected boolean isRouteDisplayed() {
    return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_S) {
        mapView.setSatellite(!mapView.isSatellite());
        return true;
    }
    return super.onKeyDown(keyCode, event);
    }

    public void onLocationChanged(Location location) {
    lat = location.getLatitude();
    lng = location.getLongitude();
    Toast.makeText(
        getBaseContext(),
        "Location change to : Latitude = " + lat + " Longitude = "
            + lng, Toast.LENGTH_SHORT).show();
    GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
    mc.animateTo(p);
    mc.setCenter(p);
    }


    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.manita.maptest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="3" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="com.google.android.maps" />
        <activity
            android:name=".MaptestActivity"
            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>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_GPS" />
    <uses-permission android:name="android.permission.ACCESS_COARSE" />
    <uses-permission android:name="android.permission.LOCATION" />

</manifest>

这里是logcat中的错误

01-10 10:37:47.765: E/Sensors(5222): ####### akmd2 started!!!
01-10 10:37:47.770: E/SensorManager(5222): registerListener 0:AK8973 Compass
01-10 10:37:47.950: E/SensorManager(5222): =======>>>>>>> Sensor Thread RUNNING <<<============================
01-10 10:37:47.995: I/MapActivity(5222): Handling network change notification:CONNECTED
01-10 10:37:47.995: E/MapActivity(5222): Couldn't get connection factory client

由于

2 个答案:

答案 0 :(得分:1)

查看此链接 Couldn't get connection factory client

Google Group:解决方案中提到您必须为地图

添加适当的API KEY

答案 1 :(得分:1)

我遇到了同样的问题

获取gps信号时无需扩展MapActivity

然后使用Activity,转向MapActivity

虽然不知道原因:(