在ANDROID中,如何使用GPS获取当前位置并在地图中跟踪而不在程序中提供任何位置?

时间:2011-05-17 07:10:34

标签: android gps

我是android的新手,任何人都可以帮我解决我的问题.... 如何使用GPS获取当前位置并在地图中跟踪而不在程序中给出任何位置?????

3 个答案:

答案 0 :(得分:1)

你想要轻松出路!使用MyLocationOverlay对象。您可以通过调用getLastFix();方法获取当前位置。要启用跟踪,请使用enableMyLocation()。要将此对象添加到地图,您需要将其添加到地图叠加层。

MyLocationOverlay currLoc=new MyLocationOverlay(context,mapKey);
mapView.getAllOverlays.add(currLoc);
currLoc.enableMyLocation();
Location myLastLocation=currLoc.getLastFix();
currLoc.enableCompass();

请确保在onPause()中执行此操作:

currLoc.disableMyLocation();  //to save battery.

您可以致电onResume()

,在currLoc.enableMyLocation();恢复更新

这是我能找到的最简单的方法!它也很准确

答案 1 :(得分:0)

尝试;

          String m_BestProvider;
          LocationManager m_LocationManager;
          LocationListener m_LocationListener = null;
          Location m_Location = null;

          m_LocationManager = (LocationManager) m_Context.getSystemService(Context.LOCATION_SERVICE);

          Criteria c = new Criteria();
          c.setAccuracy(Criteria.ACCURACY_COARSE);
          c.setAltitudeRequired(false);
          c.setBearingRequired(false);
          c.setSpeedRequired(false);
          c.setCostAllowed(true);
          c.setPowerRequirement(Criteria.POWER_HIGH);

                    m_BestProvider = m_LocationManager.getBestProvider(c, false);

                    // Define a listener that responds to location updates
                    m_LocationListener = new LocationListener() {
                        public void onLocationChanged(Location location) {
                          // Called when a new location is found by the network location provider.
                        }

                        public void onStatusChanged(String provider, int status, Bundle extras) {}

                        public void onProviderEnabled(String provider) {}

                        public void onProviderDisabled(String provider) {}
                      };

                    m_LocationManager.requestLocationUpdates(m_BestProvider, 0, 0, m_LocationListener);
                    m_Location = m_LocationManager.getLastKnownLocation(m_BestProvider);
                    Systme.out.println(m_Location.getLatitude() "," +m_Location.getLongitude());

添加AndriodManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

答案 2 :(得分:0)

public class GPSLocationBased extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.locationbased);

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    LocationListener ll = new Mylocationlistener();

    // ---Get the status of GPS---
    boolean isGPS = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);

    // If GPS is not enable then it will be on
    if(!isGPS)
    {
        Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
        intent.putExtra("enabled", true);
         sendBroadcast(intent);
    }

    //<--registers the current activity to be notified periodically by the named provider. Periodically,
    //the supplied LocationListener will be called with the current Location or with status updates.-->
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}

/**
 *Mylocationlistener class will give the current GPS location 
 *with the help of Location Listener interface 
 */
private class Mylocationlistener implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            // ---Get current location latitude, longitude, altitude & speed ---

            Log.d("LOCATION CHANGED", location.getLatitude() + "");
            Log.d("LOCATION CHANGED", location.getLongitude() + "");
            float speed = location.getSpeed();
            double altitude = location.getAltitude();
            Toast.makeText(GPSLocationBased.this,"Latitude = "+
                    location.getLatitude() + "" +"Longitude = "+ location.getLongitude()+"Altitude = "+altitude+"Speed = "+speed,
                    Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
}

}

通过这段代码,你将得到你的lat和long。 你可以在你的gmap上使用它。 此代码也会启动gps功能。希望这会对你有所帮助..一切顺利:)