首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >android - ProgressDialog加载谷歌地图

android - ProgressDialog加载谷歌地图
EN

Stack Overflow用户
提问于 2013-06-06 10:33:32
回答 3查看 4.1K关注 0票数 0

我有一个活动,实现谷歌地图我启动它,该活动停止了几秒钟,直到地图完全加载。我希望在地图不加载之前使用ProgressDialog,但我不能在后台线程中启动它,因为地图必须在主线程中加载,如this link中所述。

不用AsyncTask怎么做?

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

这是onCreate方法的代码:

代码语言:javascript
运行
复制
    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();
        }
    });
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-06-09 09:43:15

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

我所要做的就是在这段代码中出现"setContentView(R.layout.main);“调用的地方加载地图和标记。

票数 0
EN

Stack Overflow用户

发布于 2013-06-06 10:46:13

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

希望这能有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2018-05-02 19:16:58

遵循以下步骤:

1)实现GoogleMap.OnMapLoadedCallback.

地图准备就绪时的回调接口。

2) onCreate方法中的ProgressDialog代码。

//显示进度

3)onMapReady方法

代码语言:javascript
运行
复制
@Override
    public void onMapReady(GoogleMap googleMap) {
        Log.d(TAG, "OnMapReady");
        mMap = googleMap;
        mMap.setOnMapLoadedCallback(this);

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

代码语言:javascript
运行
复制
public void onMapLoaded() {
       // hide progress

    }

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16952830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档