MapActivity中的问题

时间:2011-06-22 13:46:21

标签: android google-maps

我正在测试使用google maps api的应用。我的应用程序正在运行,但我的问题是:

我希望gps找到我的位置并呈现它。如果我的gps已启用,它需要几秒钟才能找到我的位置。在这几秒钟内,我的应用程序启动并使用默认的长和lat ..如何添加像我的位置找到的进度条之类的东西?谢谢

这是我的代码的一部分:

  private double locationLat=37.979116; 
        private double locationLon=23.717766;
     MapView mapView; 
        MapController mc;
        GeoPoint p;
    //..........
    //in the onCreate

        LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            LocationListener mlocListener = new MyLocationListener();
            mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,0,0, mlocListener);

            MapView mapView = (MapView) findViewById(R.id.mapview);
            mapView.setBuiltInZoomControls(true);
            // mapView.setSatellite(true);
            // mapView.setStreetView(true);



            mc = mapView.getController();

           String lata = String.valueOf(locationLat);
            String lnga = String.valueOf(locationLon);

           String coordinates[] = {lata, lnga};
            double lat = Double.parseDouble(coordinates[0]);
            double lng = Double.parseDouble(coordinates[1]);

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

            mc.animateTo(p);
            mc.setZoom(17); 

          //---Add a location marker---
            MapOverlay mapOverlay = new MapOverlay();
            List<Overlay> listOfOverlays = mapView.getOverlays();
            listOfOverlays.clear();
            listOfOverlays.add(mapOverlay);        

            mapView.invalidate();

    //out of onCreate


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

                    //---translate the GeoPoint to screen pixels---
                    Point screenPts = new Point();
                    mapView.getProjection().toPixels(p, screenPts);

                    //---add the marker---
                    Bitmap bmp = BitmapFactory.decodeResource(
                        getResources(), R.drawable.avatar);            
                    canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
                    return true;
                }

                @Override
                public boolean onTouchEvent(MotionEvent event, MapView mapView) 
                {   
                    //---when user lifts his finger---
                    if (event.getAction() == 1) {                
                        GeoPoint p = mapView.getProjection().fromPixels(
                            (int) event.getX(),
                            (int) event.getY());

                        Geocoder geoCoder = new Geocoder(
                            getBaseContext(), Locale.getDefault());
                        try {
                            List<Address> addresses = geoCoder.getFromLocation(
                                p.getLatitudeE6()  / 1E6, 
                                p.getLongitudeE6() / 1E6, 1);

                            String add = "";
                            if (addresses.size() > 0) 
                            {
                                for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); 
                                     i++)
                                   add += addresses.get(0).getAddressLine(i) + "\n";
                            }

                            Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
                        }
                        catch (IOException e) {                
                            e.printStackTrace();
                        }   
                        return true;
                    }
                    else                
                        return false;
                }        

            } 

        @Override
        protected boolean isRouteDisplayed() {
            return false;
        }
        /* Class My Location Listener */

        public class MyLocationListener implements LocationListener

        {



        public void onLocationChanged(Location loc)

        {

        loc.getLatitude();

        loc.getLongitude();

        //String Text = "My current location is: " +

        //"Latitud = " + loc.getLatitude() +

        //"Longitud = " + loc.getLongitude();

    locationLat=loc.getLatitude();
    locationLon=loc.getLongitude();
    //  Toast.makeText( getApplicationContext(),
    //
    //  Text,
    //
    //  Toast.LENGTH_SHORT).show();

        }




        public void onProviderDisabled(String provider)

        {

        Toast.makeText( getApplicationContext(),

        "Gps Disabled",

        Toast.LENGTH_SHORT ).show();

        }




        public void onProviderEnabled(String provider)

        {

        Toast.makeText( getApplicationContext(),

        "Gps Enabled",

        Toast.LENGTH_SHORT).show();

        }


        public void onStatusChanged(String provider, int status, Bundle extras)

        {


        }



        }/* End of Class MyLocationListener */


        }/* End of Activity */

2 个答案:

答案 0 :(得分:2)

我只是把它放入一个ASyncTask然后弹出一个对话框,说找到你的位置...一旦找到就关闭对话框... Google ASyncTask示例......这应该可以帮到你。

答案 1 :(得分:0)

^^同意不确定您是否只是更新叠加层或调用地图控制器以跟随更新的纬度和经度,但这是您可以使用的想法。

另请阅读“requestLocationUpdates”的文档0,0将DESTROY一个真正的手机电池。即每0毫秒和/或0英尺更换一次更新位置。

相关问题