0

如果手机连接到 Wifi 或移动网络,我想获取我的位置。但是,如果手机在移动网络上,事情就不会按需要运行。在我启用 GPS 之前什么都没有发生。我可以在不启用 GPS 的情况下获取位置还是必须这样做?如果必须使用 GPS,我怎么能警告用户启用 GPS?

private void setUpMap() {
    // TODO Auto-generated method stub
    mMap.setMyLocationEnabled(true);

    LocationManager manager = (LocationManager)getActivity().getSystemService(FragmentActivity.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = manager.getBestProvider(criteria, true);

    myLocation = manager.getLastKnownLocation(provider);

    latitude = myLocation.getLatitude();
    longitude = myLocation.getLongitude();
    LatLng latlng = new LatLng(latitude, longitude);

    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 14));
}
4

2 回答 2

1

请参考这个Demo

您必须创建一个GPSTracker 服务来跟踪您的最后位置。

如果您专注于演示,我认为 100% 可以解决您的问题。

如果像魅力一样工作,请接受答案。

于 2013-08-26T04:56:03.560 回答
0

You have a very loaded question.

Can i get location without enabling GPS or i have to do that ? 

Of course you can. Location comes from Location Providers. Examples includes Network, Wifi and GPS. You do not need all 3. Just any one will do.

And if GPS is must, how could i warn user to enable GPS ?

You can use the method isProviderEnabled (String provider) under LocationManager Class to check if the GPS Provider is enabled. If you u can show some message and a link to enable it.

To learn how to make your app location aware, I suggest you follow the official notes here. Thats how I learnt it as well.

于 2013-08-26T01:04:21.367 回答