0

Google マップを実装するアクティビティがあります。開始すると、マップが完全に読み込まれるまで、アクティビティが数秒間停止します。マップがロードされなくなるまで ProgressDialog を使用したいのですが、このリンクで説明されているように、マップはメイン スレッドでロードする必要があるため、バックグラウンド スレッドで開始することはできません。

AsyncTask を使用せずに作成するにはどうすればよいですか?

それ以外の場合、アクティビティをすぐに開始して、ロードされていない地図を Google マップ アプリケーションのように灰色の背景で表示する方法はありますか?

これが 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();
        }
    });
}
4

3 に答える 3

0

私はついに問題を解決しました!このチュートリアルにあるコードを使用しました。

私がしなければならなかったのは、「setContentView(R.layout.main);」が存在するこのコードのポイントにマップとマーカーをロードすることだけです。電話。

于 2013-06-09T01:43:14.627 に答える