簡體   English   中英

Android Locationlistener在Android設備上不起作用

[英]Android locationlistener doesn't work on android device

當我在模擬器中使用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);

    }

implements 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;
    }

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

也許您正在門內測試,無法獲取gps信息。 嘗試更改LocationManager.GPS_PROVIDER-> LocationManager.NETWORK_PROVIDER。

我遇到了類似的問題,請嘗試編程中的所有內容。 我了解到該Google使用相同的其他傳感器來查找位置。 我什至在谷歌地圖上找不到位置偵聽器的手機。 當我檢查設置時,我同意打開電池節電功能並啟動位置監聽器。 在某些位置也不起作用的其他設備上,我打開了黃油保存功能,並顯示了位置信息。 我不知道為什么,但是黃油可能會節省您的應用程序。

這是因為當您的設備找不到位置時,它將返回默認位置0,0。 最好通過檢查其他應用(例如設備的地圖應用)來檢查設備的GPS連接

如果GPS無法找到信號,則Google地圖將默認為網絡位置提供商,以便測試可以返回沒有GPS信號的位置。 我會建議GPS狀態作為一個很好的免費應用程序。 我能想到的唯一的另一件事是確保您對天空有清晰的視野。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM