Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法使用谷歌地图获取位置_Android_Google Maps_Geolocation_Emulation - Fatal编程技术网

Android 无法使用谷歌地图获取位置

Android 无法使用谷歌地图获取位置,android,google-maps,geolocation,emulation,Android,Google Maps,Geolocation,Emulation,我正在为一个android项目寻找我的当前位置。加载应用程序时,我的当前位置始终为空。我已在清单等中设置了权限。当我找到当前位置时,我打算使用坐标查找到地图上其他位置的距离。下面是我的代码片段。为什么我总是得到一个空值? 这是我的密码:- package com.example.location; import android.content.Context; import android.location.LocationManager; import andr

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

    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) {

        }

    }

看看这个例子。做你想做的事情的很好的例子


试试这个,我已经厌倦了你的链接,但问题仍然存在,它输入了位置==null plz请帮助我,我已经厌倦了,请你在模拟器上输入DDMS的长度和纬度,以获得你最后已知的定位方法。如果没有,您需要通过DDMS提供以前的位置。