2011-02-03 82 views
2

嘿伙计,我需要你的帮助 嗯,我已经建立了地图应用程序,并且运行完美,现在我想查看我在当前位置的地图。如何去做呢? PLZ建议谷歌地图上的GPS坐标

回答

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