GPS提供商未在第一时间显示当前位置

时间:2014-04-21 09:11:57

标签: android google-maps gps location

我正在尝试从网络提供商和gps获取位置。通过使用返回的纬度和经度,我在地图上放置标记。当我尝试使用网络提供商应用程序工作正常。但是当我试图从gps获取位置时,第一次返回null 。之后,它给了我确切的位置结果。如何摆脱这个。?

我认为我必须做一些小改动,请建议我解决这个问题。

这就是我这样做的方式。

    public class MapActivity extends Activity implements LocationListener {


            private GoogleMap gMap;
            private LocationManager locationManager;
            private String provider;
            boolean gps_enabled = false;
            boolean netwrk_enabled = false;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_map);
                gMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                        .getMap();

    //Focussing india on starting map
                gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
                        23.40276491, 77.51953125), 5));

                locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000L, 10F, this);

                gps_enabled = locationManager
                        .isProviderEnabled(LocationManager.GPS_PROVIDER);

                netwrk_enabled = locationManager
                        .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
                if (gps_enabled)
                    provider = LocationManager.GPS_PROVIDER;
                else
                    provider = LocationManager.NETWORK_PROVIDER;

                Criteria criteria = new Criteria();
                provider = locationManager.getBestProvider(criteria, false);

                Location location = locationManager.getLastKnownLocation(provider);



    CameraPosition cp = new CameraPosition(new LatLng(
                        location.getLatitude(), location.getLongitude()), 14, 40, 90);
                gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cp), 4000,
                        null);
                gMap.addMarker(new MarkerOptions().position(
                        new LatLng(location.getLatitude(), location.getLongitude()))
                        .title("I am here"));

                if (location != null) {

                    if (provider == LocationManager.GPS_PROVIDER)
                        Toast.makeText(getApplicationContext(), "taking from gps",
                                Toast.LENGTH_SHORT).show();

                    else if (provider == LocationManager.NETWORK_PROVIDER)
                        Toast.makeText(getApplicationContext(), "taking from network",
                                Toast.LENGTH_SHORT).show();

                    new HttpGetTask().execute();
                }

            }

            @Override
            protected void onPause() {
                // TODO Auto-generated method stub18d1:d002
                super.onPause();
                locationManager.removeUpdates(this);
            }

            @Override
            protected void onResume() {
                // TODO Auto-generated method stub
                super.onResume();
                locationManager.requestLocationUpdates(provider, 400, 1, this);
            }

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.map, menu);
                return true;
            }

            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub
                Toast.makeText(this, "Disabled provider " + provider,
                        Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

                Toast.makeText(this, "Enabled new provider " + provider,
                        Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub

            }

            private class HttpGetTask extends AsyncTask<Void, Void, String> {

        //Passing latitude and longitude here to get lat longs from my remote server.
        // using json parsing i am placing multiple markers here.


            }

        }

1 个答案:

答案 0 :(得分:0)

您编写的所有内容都是写的,但您不是在启动Application时调用LocationManager。您只需在onCreate()方法的代码中注释以下行。

//Location location = locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000L, 10F, this);

取消注释以上行并对提供商进行以下更改

Location location = locationManager.requestLocationUpdates(provider, 5000L, 10F, this);