Android MapActivity中的问题

Android MapActivity中的问题,android,google-maps,Android,Google Maps,我正在测试一个使用谷歌地图api的应用程序。我的应用程序正在运行,但我的问题是: 我希望gps找到我的位置并显示它。如果我的gps处于启用状态,它需要几秒钟的时间来找到我的位置。在这几秒钟内,我的应用程序启动并使用默认的long和lat。在找到我的位置之前,我如何添加进度条之类的内容?谢谢 这是我代码的一部分: private double locationLat=37.979116; private double locationLon=23.717766; M

我正在测试一个使用谷歌地图api的应用程序。我的应用程序正在运行,但我的问题是:

我希望gps找到我的位置并显示它。如果我的gps处于启用状态,它需要几秒钟的时间来找到我的位置。在这几秒钟内,我的应用程序启动并使用默认的long和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 */
private double locationLat=37.979116;
私人双位置LON=23.717766;
地图视图;
地图控制器;
地质点p;
//..........
//在onCreate中
LocationManager mlocManager=(LocationManager)getSystemService(Context.LOCATION\u服务);
LocationListener mlocListener=新建MyLocationListener();
mlocManager.RequestLocationUpdate(LocationManager.GPS_提供程序,0,0,mlocListener);
MapView MapView=(MapView)findViewById(R.id.MapView);
mapView.SetBuilTinZoomControl(真);
//mapView.setSatellite(真);
//mapView.setStreetView(真);
mc=mapView.getController();
String lata=String.valueOf(locationLat);
String lnga=String.valueOf(locationLon);
字符串坐标[]={lata,lnga};
double lat=double.parseDouble(坐标[0]);
double lng=double.parseDouble(坐标[1]);
p=新的地质点(
(内部)(纬度*1E6),
(国际)(液化天然气*1E6));
司马迁(p),;
mc.setZoom(17);
//---添加位置标记---
MapOverlay MapOverlay=新的MapOverlay();
List ListoForLays=mapView.getOverlays();
listOfOverlays.clear();
添加(映射覆盖);
mapView.invalidate();
//一经创建
类MapOverlay扩展了com.google.android.maps.Overlay
{
@凌驾
公共布尔绘制(画布、地图视图、地图视图、,
布尔阴影(长时)
{
super.draw(画布、地图视图、阴影);
//---将地质点转换为屏幕像素---
点屏幕PTS=新点();
getProjection().toPixels(p,screenPts);
//---添加标记---
位图bmp=BitmapFactory.decodeResource(
getResources(),R.drawable.avatar);
canvas.drawBitmap(bmp,screenPts.x,screenPts.y-50,null);
返回true;
}
@凌驾
公共布尔onTouchEvent(MotionEvent事件,MapView MapView)
{   
//---当用户抬起手指时---
如果(event.getAction()==1){
地理点p=mapView.getProjection().fromPixels(
(int)event.getX(),
(int)event.getY();
地理编码器地理编码器=新地理编码器(
getBaseContext(),Locale.getDefault();
试一试{
列表地址=geoCoder.getFromLocation(
p、 getLatitudeE6()/1E6,
p、 getLongitudeE6()/1E6,1);
字符串add=“”;
如果(地址.size()>0)
{

对于(int i=0;i^^^^我同意这一点,但不确定您是否只是在更新覆盖或调用地图控制器来跟踪更新的lat和lon,但这是您可以使用的想法


另外,请阅读“RequestLocationUpdate”文档0,0会破坏一个真正的手机电池。这是每0毫秒和/或0英尺变化一次更新位置。

我会把它放入一个异步任务中,然后弹出一个对话框,说找到你的位置…一旦找到,关闭对话框…谷歌异步任务示例…这应该会帮助你解决问题。

考虑到没有已知数量的time在设备能够检测到其GPS位置之前,进度条可能不是您真正想要的。