【问题标题】:Cannot fetch Location using google maps无法使用谷歌地图获取位置
【发布时间】:2012-12-16 18:18:59
【问题描述】:

我正在尝试为一个 android 项目查找我当前的位置。加载应用程序时,我的当前位置始终为空。我已经在清单等中设置了权限。当我找到当前位置时,我打算使用坐标来查找到地图上其他位置的距离。我的代码 sn-p 如下。为什么我总是得到一个空值? 这是我的代码:-

    package com.example.location;
    import android.content.Context;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.location.Location;
    import com.google.android.maps.MapActivity;
    import com.google.android.maps.MapView;
    import android.location.LocationListener;
    import com.google.android.maps.MapController;
    import android.widget.Toast;
    import com.google.android.maps.GeoPoint;

    public class MainActivity extends MapActivity implements LocationListener {

        private MapView mapView;
        private LocationManager locManager;

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState); 
            setContentView(R.layout.activity_main);

            //fetch the map view from the layout
            MapView mapView = (MapView) findViewById(R.id.mapview);

            //make available zoom controls 
            mapView.setBuiltInZoomControls(true);
            //latitude and longitude of Rome
            double lat = 33.6667;
            double lon = 73.1667;

            //create geo point
            GeoPoint point = new GeoPoint((int)(lat * 1E6), (int)(lon *1E6));

            //get the MapController object
            MapController controller = mapView.getController();

            //animate to the desired point
            controller.animateTo(point); 

            //set the map zoom to 13
            // zoom 1 is top world view
            controller.setZoom(13);

            // invalidate the map in order to show changes
            **mapView.invalidate();
            // Use the location manager through GPS
            locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,
                    0, this);
            //get the current location (last known location) from the location manager
            Location location = locManager
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

            if (location != null) {

                Toast.makeText(
                        this,
                        "Current location:\nLatitude: " + location.getLatitude()
                                + "\n" + "Longitude: " + location.getLongitude(),
                        Toast.LENGTH_LONG).show(); }
                else {

                    Toast.makeText(this, "Cannot fetch current location!",
                            Toast.LENGTH_LONG).show();
                }**
              //when the current location is found – stop listening for updates (preserves battery)
                locManager.removeUpdates(this);
        }

        @Override
        protected boolean isRouteDisplayed() {

            return false;

        }
        @Override
        protected void onPause() {
            super.onPause();
            locManager.removeUpdates(this); //activity pauses => stop listening for updates
        }
            @Override
        public void onLocationChanged(Location location) {

        }


        @Override
        public void onProviderDisabled(String provider) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

    }

【问题讨论】:

标签: android google-maps geolocation emulation


【解决方案1】:

【讨论】:

  • 我的链接已经累了,但问题仍然存在,它进入了 location == null 的地方,请帮助我,我累了
  • 您是否在您的模拟器上输入了来自 DDMS 的经度和纬度,用于您最后一次已知的定位方法。如果没有,您需要通过 DDMS 提供任何以前的位置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多