Android locationlistener无法在Android设备上运行

时间:2021-11-05 21:13:43

My location listener works when I am using the DDMS controls in the emulator but when I deploy it onto the phone it no longer works. My code is as follows

当我在模拟器中使用DDMS控件时,我的位置监听器工作,但当我将其部署到手机上时,它不再有效。我的代码如下

public class hoosheerAndroidAct extends MapActivity implements LocationListener {
    /** Called when the activity is first created. */
    MapController mc;
    public static MapView gMapView = null;
    GeoPoint p = null;
    static double latitude, longitude;
    MyLocationOverlay myLocationOverlay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen1);

        // Creating and initializing Map
        gMapView = (MapView) findViewById(R.id.mapview);
        p = new GeoPoint((int) (latitude * 1E6),
                (int) (longitude * 1E6));
        gMapView.setSatellite(true);
        gMapView.setBuiltInZoomControls(true);
        mc = gMapView.getController();
        mc.setCenter(p);

        mc.setZoom(11);
        myLocationOverlay = new MyLocationOverlay(this, gMapView);
        gMapView.getOverlays().add(myLocationOverlay);
        list = gMapView.getOverlays();
        list.add(myLocationOverlay);

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 5.0f,
                this);

    }

methods implemented for implements LocationListener

为实现LocationListener实现的方法

public void onLocationChanged(Location location) {
        if (location != null) {
            GeoPoint point = new GeoPoint((int) (location.getLatitude() * 1E6),
                    (int) (location.getLongitude() * 1E6));
            mc.animateTo(point);
            connect("http://api.foursquare.com/v1/venues.json?geolat="
                    + location.getLatitude() + "&geolong="
                    + location.getLongitude());
            Drawable marker = getResources().getDrawable(R.drawable.marker);
            gMapView.getOverlays().add(new SitesOverlay(marker));

        }
    }

    public void onProviderDisabled(String provider) {
        // required for interface, not used
    }

    public void onProviderEnabled(String provider) {
        // required for interface, not used
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // required for interface, not used
    }

    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

Is there anything I am missing? :-(

有什么我想念的吗? :-(

4 个解决方案

#1


0  

Google maps will default to network location provider if the GPS is unable to find a signal so that test could return a location without a GPS signal. I would recommend GPS Status as a good free app. The only other thing I can think of is to make sure you have a clear view of the sky.

如果GPS无法找到信号,Google地图将默认为网络位置提供商,以便测试可以返回没有GPS信号的位置。我会推荐GPS状态作为一个很好的免费应用程序。我唯一能想到的就是确保你能清楚地看到天空。

#2


0  

its because when your device is not able to find location it return default location 0,0. better you check your device gps connection by checking other apps like map app of your device

因为当您的设备无法找到位置时,它会返回默认位置0,0。通过检查设备的地图应用等其他应用,您可以更好地检查设备的GPS连接

#3


0  

Maybe you are testing in door,can't get gps information. try to change LocationManager.GPS_PROVIDER-->LocationManager.NETWORK_PROVIDER.

也许你在门口测试,无法获得gps信息。尝试更改LocationManager.GPS_PROVIDER - > LocationManager.NETWORK_PROVIDER。

#4


0  

I had a similar problem and try everything in programming. I read about that google use same other sensors to find location. I even find mobile where location listener won't work in google maps. When I checked the settings i accedently turn on battery power saving and location listener starts. On some other device where location wan't work too I turn on buttery saving and location is shown. I don't know why is that, but buttery maybe will save your app.

我有类似的问题,并尝试编程中的一切。我读到谷歌使用相同的其他传感器来查找位置。我甚至找到了位置监听器无法在谷歌地图中工作的移动设备。当我检查设置时,我继续打开电池省电并启动位置监听器。在其他位置也不能工作的设备上,我打开黄油保存并显示位置。我不知道为什么会这样,但是黄油可能会保存你的应用程序。

#1


0  

Google maps will default to network location provider if the GPS is unable to find a signal so that test could return a location without a GPS signal. I would recommend GPS Status as a good free app. The only other thing I can think of is to make sure you have a clear view of the sky.

如果GPS无法找到信号,Google地图将默认为网络位置提供商,以便测试可以返回没有GPS信号的位置。我会推荐GPS状态作为一个很好的免费应用程序。我唯一能想到的就是确保你能清楚地看到天空。

#2


0  

its because when your device is not able to find location it return default location 0,0. better you check your device gps connection by checking other apps like map app of your device

因为当您的设备无法找到位置时,它会返回默认位置0,0。通过检查设备的地图应用等其他应用,您可以更好地检查设备的GPS连接

#3


0  

Maybe you are testing in door,can't get gps information. try to change LocationManager.GPS_PROVIDER-->LocationManager.NETWORK_PROVIDER.

也许你在门口测试,无法获得gps信息。尝试更改LocationManager.GPS_PROVIDER - > LocationManager.NETWORK_PROVIDER。

#4


0  

I had a similar problem and try everything in programming. I read about that google use same other sensors to find location. I even find mobile where location listener won't work in google maps. When I checked the settings i accedently turn on battery power saving and location listener starts. On some other device where location wan't work too I turn on buttery saving and location is shown. I don't know why is that, but buttery maybe will save your app.

我有类似的问题,并尝试编程中的一切。我读到谷歌使用相同的其他传感器来查找位置。我甚至找到了位置监听器无法在谷歌地图中工作的移动设备。当我检查设置时,我继续打开电池省电并启动位置监听器。在其他位置也不能工作的设备上,我打开黄油保存并显示位置。我不知道为什么会这样,但是黄油可能会保存你的应用程序。