获取当前位置的纬度和经度时出现问题

发布于 2024-11-17 13:19:42 字数 2716 浏览 3 评论 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;
        }
    } 

}

i want to get latitude and longitude by using gps ..my code is works successfully but it works too slow.
how to get geopoint (latitude and longitude) minimun time duration (20 sec.) ? and how to stop location service or GPS after 30 sec.?

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

}

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

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

发布评论

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

评论(1

能否归途做我良人 2024-11-24 13:19:42

查看这篇文章。这应该可以帮助您了解如何更好地使用位置。

要在 30 秒后停止定位,您可以启动计时器并在该时间后通过调用停止 LocationService

 locationManager.stopUpdates(LocationListener).

Check this post. This should help you understand how to use location better.

For stopping the location after 30 seconds you may start a timer and stop the LocationService after that time by calling

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