地图无法获取当前位置

时间:2014-11-07 12:20:42

标签: android google-maps

当我启动我的应用程序上有一个地图时,它应该获取设备的当前位置并将地图聚焦到该位置,但事实并非如此。这就是我这样做的方式:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.location_layout, container, false);

    checkMap();
    return view;
}

private void checkMap() {
    if (mMap == null) {
        /*Try to obtain the map from the SupportMapFragment*/
        mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        /*Check if we were successful in obtaining the map.*/
        if (mMap != null) {
            initMap();
        }
    }
}

private void initMap() {
    LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    String provider = locationManager.getBestProvider(criteria, true);
    /*Get last known location*/
    defaultLocation = locationManager.getLastKnownLocation(provider);
    /*If it doesn't exist, update once*/
    if (defaultLocation == null) {
        locationManager.requestSingleUpdate(criteria, new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                defaultLocation = location;
            }
            @Override
            public void onProviderEnabled(String provider) {
            }
            @Override
            public void onProviderDisabled(String provider) {
            }
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
        }, getActivity().getMainLooper());
    }
    /*When we get the location, focus the map on it*/
    if (defaultLocation != null) {
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                new LatLng(defaultLocation.getLatitude(), defaultLocation.getLongitude()), 5));
    }
}

我没有遇到问题,它只是没有将地图集中在当前位置。我做错了什么?

清单:

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- The following two permissions are not required to use
    Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

<meta-data 
    android:name="com.google.android.gms.version" 
    android:value="@integer/google_play_services_version" />       
<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="...KEY..."/> 

3 个答案:

答案 0 :(得分:1)

尝试以下代码:

Criteria criteria = new Criteria();
        LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        List<String> providers = locationManager.getProviders(true);
        for (String provider : providers) {
            locationManager.requestLocationUpdates(provider, 1000, 0,
                    new LocationListener() {
                        public void onLocationChanged(Location location) {
                        }

                        public void onProviderDisabled(String provider) {
                        }

                        public void onProviderEnabled(String provider) {
                        }

                        public void onStatusChanged(String provider,
                                int status, Bundle extras) {
                        }
                    });
            Location location = locationManager.getLastKnownLocation(provider);
            if (location != null) {
                double lat = location.getLatitude();
                double lng= location.getLongitude();

            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom( new LatLng( lat, lng), 15));
            // Zoom in, animating the camera.
         googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 1500, null);
}
}
它对我有用。 多数民众赞成......

答案 1 :(得分:0)

检查您是否获得了正确的纬度经度,您可以打印纬度和经度并尝试此操作。

LatLng latLng = new LatLng(latitude, longitude); // put your lat long here

CameraUpdate center = CameraUpdateFactory.newLatLng(latLng);
CameraUpdate zoom = CameraUpdateFactory.zoomTo(10);

googleMap.moveCamera(center);
googleMap.animateCamera(zoom);

答案 2 :(得分:0)

更改

if (defaultLocation != null) {
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                new LatLng(defaultLocation.getLatitude(), defaultLocation.getLongitude()), 5));
    }

requestSingleUpdate听众内部:

        @Override
        public void onLocationChanged(Location location) {
            defaultLocation = location;
             mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                new LatLng(defaultLocation.getLatitude(), defaultLocation.getLongitude()), 5));
        }