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

发布于 2024-11-08 01:35:57 字数 76 浏览 0 评论 0原文

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

I am new to android, can anyone help me for my question....
How to get a current position and tracking in map using GPS without giving any location in a program?????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落花浅忆 2024-11-15 01:35:57

你想要简单的出路!使用 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.

可以通过调用 currLoc.enableMyLocation();onResume() 中恢复更新

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

You want the easy way out ! Use MyLocationOverlay object. you can get the current location by calling the method getLastFix(); . To enable tracking use enableMyLocation(). To add this object to the map you need to add it to your map overlays.

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

Do make sure, in onPause() you do this :

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

you can resume updates in onResume() by calling currLoc.enableMyLocation();

This is the easiest way I could find! and it is quite accurate too

回眸一遍 2024-11-15 01:35:57

尝试 ;

          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"/>

Try ;

          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());

Add in 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"/>
萌面超妹 2024-11-15 01:35:57
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) {
    }
}

通过这段代码

你将得到你的纬度和经度。
你可以在你的 gmap 上使用它。
此代码也有问题地启动 GPS 功能。希望这会帮助你..祝一切顺利:)

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) {
    }
}

}

Through this code u will get ur lat and long.
and u may use it on ur gmap.
this code also start gps functionality problematically. Hope this will help u.. All the best :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文