【问题标题】:android - ProgressDialog while loading Google Mapsandroid - 加载谷歌地图时的 ProgressDialog
【发布时间】:2018-05-02 19:16:57
【问题描述】:

我有一个实现谷歌地图的活动。当我启动它时,活动会停止几秒钟,直到地图完全加载。 我想使用 ProgressDialog 直到地图未加载,但我无法在后台线程中启动它,因为地图必须在主线程中加载,如 this link 中所述。

如何在不使用 AsyncTask 的情况下完成它?

否则,有没有办法像谷歌地图应用程序一样立即启动活动并以灰色背景显示未加载的地图?

这是onCreate方法的代码:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mappa);
    databaseHelper = new MyDatabaseHelper(this);
    Bundle b = getIntent().getExtras();
    String placeAddress= "";
    if(b != null)
        placeAddress= b.getString("indirizzo");

    setUpMapIfNeeded();
    gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    gMap.setMyLocationEnabled(true);
    UiSettings settings = gMap.getUiSettings();
    settings.setMyLocationButtonEnabled(true);

    LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);
    double lat = location.getLatitude();
    double lng = location.getLongitude();
    position = new LatLng(lat, lng);

    if(!placeAddress.equals("")){
        Geocoder geocoder = new Geocoder(this, Locale.getDefault());
        List<Address> indirizzi = null;
        try {
            indirizzi = geocoder.getFromLocationName(placeAddress, 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        double latLuogo = indirizzi.get(0).getLatitude();
        double lngLuogo = indirizzi.get(0).getLongitude();
        LatLng luogo = new LatLng(latLuogo, lngLuogo);
        CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(luogo)
            .zoom(15)
            .build();
        gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    }
    else{
        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(position)
                .zoom(15)
                .build();
        gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    }

    LocationListener listener = new LocationListener() {
            public void onLocationChanged(Location location){
                //makeUseOfNewLocation(location);
            }

            @Override
            public void onProviderDisabled(String arg0) {}

            @Override
            public void onProviderEnabled(String arg0) {}

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    locationManager.requestLocationUpdates(provider, 0, 10, listener);

    ConnectivityManager connMngr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo netInfo = connMngr.getActiveNetworkInfo();

    if(checkConnection(netInfo) == true ){
    loadFromDatabase();   //load markers

    gMap.setInfoWindowAdapter(new InfoWindowAdapter(){

        @Override
        public View getInfoWindow(Marker marker) {
            return null;
        }

        @Override
        public View getInfoContents(Marker marker) {
            String nome = marker.getTitle();
            String indirizzo = marker.getSnippet();
            View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
            TextView title = (TextView)v.findViewById(R.id.titleInfoWindow);
            title.setText(nome);
            TextView snippet = (TextView)v.findViewById(R.id.snippetInfoWindow);
            snippet.setText(indirizzo);
            ImageView imView = (ImageView)v.findViewById(R.id.info_windowImageView);
            impostaImmagine(imView, nome);
            return v;
        }
    });

    gMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {

        @Override
        public void onInfoWindowClick(Marker marker) {
            final String nome = marker.getTitle();
            final String indirizzo = marker.getSnippet();
            startLuogoActivity(nome, indirizzo);
        }
    });

    final Geocoder geocoder = new Geocoder(this, Locale.getDefault());

    gMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {

        @Override
        public void onMapLongClick(LatLng point) {
            List<Address> addresses = null;
            if(checkConnection(netInfo) == true){
                try {
                    addresses = geocoder.getFromLocation(point.latitude, point.longitude, 1);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if(addresses!=null){
                    String address = addresses.get(0).getAddressLine(0);
                    String city = addresses.get(0).getAddressLine(1);
                    String country = addresses.get(0).getAddressLine(2);
                    String indirizzo = address + ", " + city + ", " + country;
                    final Dialog addByClickDialog = onCreateDialogADDByClick(getBaseContext(), indirizzo);
                    addByClickDialog.show();
                    Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                    v.vibrate(50);
                }else{
                    final Dialog nessunaConnessioneDialog = onCreateDialogNessunaConnessione(getBaseContext());
                    nessunaConnessioneDialog.show();
                    Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                    v.vibrate(50);
                }
            }
            else{
                final Dialog nessunaConnessioneDialog = onCreateDialogNessunaConnessione(getBaseContext());
                nessunaConnessioneDialog.show();
                Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                v.vibrate(50);
            }
        }
    });

    Button addButton = (Button)findViewById(R.id.addButton);
    final Dialog addDialog;
    if(checkConnection(netInfo) == false){
        addDialog = onCreateDialogADD(getBaseContext(), false);
    }else{
        addDialog = onCreateDialogADD(getBaseContext(), true);
    }
    addButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            addDialog.show();
        }
    });

    Button deleteButton = (Button)findViewById(R.id.deleteButton);
    final Dialog deleteDialog;
    if(checkConnection(netInfo) == false){
        deleteDialog = onCreateDialogDELETE(getBaseContext(), false);
    }else{
        deleteDialog = onCreateDialogDELETE(getBaseContext(), true);
    }
    deleteButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            deleteDialog.show();
        }
    });
}

【问题讨论】:

  • 显示代码的主要部分。所以我可以帮助你..
  • 你使用的是 MapActivity 还是 MapFragment?
  • 为什么需要几秒钟?除非您在主线程上执行其他操作,否则地图加载速度很快。
  • @KunalS.Kushwah 我正在使用 MapFragment
  • @MaciejGórski 我不仅在主线程上加载地图,还从数据库中加载一些标记,设置 infoWindow 布局,一些按钮侦听器和其他东西

标签: android google-maps progressdialog


【解决方案1】:

检查@commonsware 对ProgressDialog not shown in UIThread 问题的回答。将 ProgressDialog 用于 MapActivity 可能不是一个好主意,因为显示 MapView 所需的时间主要取决于返回 Google Maps 服务器的 Internet 连接。您无法知道 MapView 何时完成加载,因此您无法知道何时关闭对话框。但是,如果您仍然非常确定它会始终加载并且花费更少的时间,那么您可以阅读更多 here。您可能还想查看指向相同链接的相关问题:Android : showDialog when setContentView loading

希望这会有所帮助。

【讨论】:

  • 是的,主要问题是互联网连接。如果我用 wifi 连接打开 MapFragment,加载并不需要太多。但如果我用 3G 连接,它可以需要几秒钟,所有应用程序都会停止响应。我会尝试您发布的链接中的解决方案,并让您知道它是否有效。
【解决方案2】:

我终于解决了问题!我使用了this tutorial中的代码。

我所要做的就是加载地图和此代码中存在“setContentView(R.layout.main);”的标记点打电话。

【讨论】:

  • 那么你是如何设法添加进度对话框的,请提前分享一下
【解决方案3】:

按照以下步骤操作:

1) 实现 GoogleMap.OnMapLoadedCallback。

地图可以使用时的回调接口。

2)onCreate 方法中的ProgressDialog 代码。

//显示进度

3)onMapReady 方法

@Override
    public void onMapReady(GoogleMap googleMap) {
        Log.d(TAG, "OnMapReady");
        mMap = googleMap;
        mMap.setOnMapLoadedCallback(this);

3)地图加载时调用onMapLoaded()。

public void onMapLoaded() {
       // hide progress

    }

请检查第 3 点,这很重要。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    相关资源
    最近更新 更多