2011-06-27 65 views
0

我想通過使用gps獲取緯度和經度..我的代碼工作成功,但工作速度太慢。 如何獲得最短持續時間(20秒)的地點(經度和緯度)?以及30秒後如何停止定位服務或GPS?獲取當前位置的緯度和經度的問題

public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     //Log.i("WHERE","ShowOnMapActivity onCreate()"); 
     setContentView(R.layout.productlistmapview); 

     button_back = (Button)findViewById(R.id.btn_back_map); 
     button_back.setOnClickListener(this); 


     map_view = (MapView) findViewById(R.id.mapview_productlist); 
     LinearLayout zoom_layout = (LinearLayout)findViewById(R.id.zoom_map); 
     View zoomView = map_view.getZoomControls(); 

     zoom_layout.addView(zoomView,new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
     map_view.displayZoomControls(true); 



     double str_latitude = XMLData.user_latitude; 
     double str_longitude =XMLData.user_logitude; 

     Log.i("str_latitude",str_latitude+""); 
     Log.i("str_longitude",str_longitude+""); 


     map_controller = map_view.getController(); 
; 
     double coordinates[] = {str_latitude, str_longitude}; 
     double lat = coordinates[0]; 
     double lng = coordinates[1]; 

     geo_point = new GeoPoint((int) (lat * 1E6),(int) (lng * 1E6)); 

     map_controller.animateTo(geo_point); 
     map_controller.setZoom(9); 



     MapOverlay map_overlay = new MapOverlay(); 
     List<Overlay> list_of_overlays = map_view.getOverlays(); 
     list_of_overlays.clear(); 
     list_of_overlays.add(map_overlay);   

     map_view.invalidate(); 

    } 

    public void onClick(View v) 
    { 
     switch(v.getId()) 
     { 
     case R.id.btn_back_map: 
       Intent back_intent = new Intent(this,GroupPurchasingSelectProduct.class); 
       startActivity(back_intent); 
       break;  
     } 
    } 
    @Override 
    protected boolean isRouteDisplayed() 
    { 
     Log.i("WHERE","ShowOnMapActivity isRouteDisplayed()"); 
     // TODO Auto-generated method stub 
     return false; 
    } 
    class MapOverlay extends com.google.android.maps.Overlay 
    { 
     @Override 
     public boolean draw(Canvas canvas, MapView mapView, 
     boolean shadow, long when) 
     { 
      super.draw(canvas, mapView, shadow);     

      Point screen_points = new Point(); 
      mapView.getProjection().toPixels(geo_point, screen_points); 


      Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pushpin);    
      canvas.drawBitmap(bitmap, screen_points.x, screen_points.y-58, null);   
      return true; 
     } 
    } 

} 
+0

代碼啓動計時器和停止時間後LocationService後停止位置不能很好地貼 – PedroAGSantos

回答

0

檢查this post。這應該會幫助你更好地理解如何使用位置。

對於30秒您可以致電

locationManager.stopUpdates(LocationListener). 
+0

感謝您的幫助... –