开发者

Problem using GPS_PROVIDER for location

I am developing an Android app which uses the current user location for result.

I am using the following code to get the location:

LocationManager locationManager =
    (LocationManager) getSystemService(LOCAT开发者_StackOverflow社区ION_SERVICE);

Location currentLocation =
    locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

When I run it on device it gives me a null value for location.

But when I use the NETWORK_PROVIDER it works fine is there any extra setting for the device to use GPS?

My device is Galaxy Tab P-1000.


Calling getLastKnownLocation() with GPS_PROVIDER returns a cached value. If there is no recent location it will return null. Use the LocationListener's onLocationChanged() callback instead.

It may take a while but you will get a fresh position fix.

NETWORK_PROVIDER's location is not always accurate because it is based on cell-site triangulation


Use:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                MINIMUM_TIME_BETWEEN_UPDATES,
                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, locationListener);

For locationListener you may use this:

LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            // Called when a new location is found by the network location
            // provider.

            String message = String.format(
                    "New Location \n Longitude: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude());
            Toast.makeText(LbsGeocodingActivity.this, message,
                    Toast.LENGTH_LONG).show();

            if (location != null) {
                GeoPoint point2 = new GeoPoint(
                        (int) (location.getLatitude() * 1E6), (int) (location
                                .getLongitude() * 1E6));

                // Toast.makeText(getBaseContext(),
                // "Latitude: " + location.getLatitude() +
                // " Longitude: " + location.getLongitude(),
                // Toast.LENGTH_SHORT).show();
                //                 
                mapView.getController().animateTo(point2);
                mapView.getController().setZoom(15);

                List<Overlay> mapOverlays = mapView.getOverlays();
                Drawable drawable = LbsGeocodingActivity.this.getResources()
                .getDrawable(R.drawable.new3pin);
                HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
                        drawable, LbsGeocodingActivity.this);
                point = new GeoPoint((int) (location.getLatitude() * 1E6),
                        (int) (location.getLongitude() * 1E6));

                String address = convertPointToLocation(point);
                String delims = ",";
                String[] tokens = address.split(delims);
                OverlayItem overlayitem = null;
                if(tokens.length==1)
                {

                    overlayitem = new OverlayItem(point,
                            "New Location", "Locality:"+"\nCity:"+"\nCountry: "+tokens[0]);
                }
                else if(tokens.length==2)
                {

                    overlayitem = new OverlayItem(point,
                            "New Location", "Locality:"+"\nCity: "+tokens[0]+"\nCountry: "+tokens[1]);
                }
                else if(tokens.length==3)
                {

                    overlayitem = new OverlayItem(point,
                            "New Location", "Locality: "+tokens[0]+"\nCity: "+tokens[1]+"\nCountry: "+tokens[2]);
                }
                else
                {
                    overlayitem = new OverlayItem(point,
                            "New Location", address);
                }

                itemizedoverlay.addOverlay(overlayitem);
                mapOverlays.clear();
                mapOverlays.add(itemizedoverlay);

                // mapView.invalidate();
            }

        }

I have used this in my code and it works well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜