谷歌地图上的Gps坐标

时间:2011-02-03 11:07:27

标签: android

嘿伙计们,我再次需要你的帮助 好吧,我已经构建了地图应用程序并且运行正常,现在我想在该地图中查看我当前的位置。怎么去呢? PLZ建议

1 个答案:

答案 0 :(得分:2)

有不同的方法,

你需要添加一个位置监听器。有两种类型,一种是使用GPS本身(这不会在室内工作),使用网络。

private LocationManager locationManager;
public LocationListener locationListener;
public LocationListener locationListener2;

locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            locationListener = new MyLocationListener();
            locationListener2 = new MyLocationListener();               
            locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 0, 0, locationListener);
            locationManager.requestLocationUpdates(locationManager.NETWORK_PROVIDER, 0, 0, locationListener2);



//Location lister
private class MyLocationListener implements LocationListener {
    public void onLocationChanged(Location loc) {

        mlongti = loc.getLongitude();
        mlatiti = loc.getLatitude();
        GeoPoint userLoc = new GeoPoint((int) (mlatiti * 1E6), (int) (mlongti * 1E6));
        LItemizedOverlay itemizedoverlay = new LItemizedOverlay(selfImage,getParent(), "",userLoc,display);

        try {           
            OverlayItem overlayitem = new OverlayItem(userLoc, "", "");
            itemizedoverlay.addOverlay(overlayitem);
        } catch (Exception e) {
            e.printStackTrace();
        }

        List<Overlay> listOfOverlays = map.getOverlays();
        listOfOverlays.add(itemizedoverlay);
        MapController mc = map.getController();         
        mc.animateTo(userLoc);
        mc.setZoom(10);
        map.invalidate();       
        if(locationManager!=null && locationListener2 != null){
            locationManager.removeUpdates(locationListener);
            locationManager.removeUpdates(locationListener2);
            locationManager= null;
        }
    }

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