Android 位置侦听器在 Android 设备上不起作用

发布于 2024-11-02 10:46:25 字数 2468 浏览 0 评论 0原文

当我在模拟器中使用 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;
    }

我还缺少什么吗? :-(

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

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

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? :-(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小女人ら 2024-11-09 10:46:25

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

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.

生生漫 2024-11-09 10:46:25

这是因为当您的设备无法找到位置时,它会返回默认位置 0,0。最好通过检查其他应用程序(例如设备的地图应用程序)来检查设备的 GPS 连接

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

生寂 2024-11-09 10:46:25

也许您正在室内测试,无法获取 GPS 信息。尝试更改LocationManager.GPS_PROVIDER-->LocationManager.NETWORK_PROVIDER。

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

离不开的别离 2024-11-09 10:46:25

我遇到了类似的问题,并尝试了编程中的所有方法。我读到谷歌使用相同的其他传感器来查找位置。我什至发现移动设备的位置侦听器无法在谷歌地图中工作。当我检查设置时,我意外地打开了电池省电功能并启动了位置侦听器。在其他一些位置也不起作用的设备上,我打开黄油保存并显示位置。我不知道为什么会这样,但黄油也许会拯救你的应用程序。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文