0

私の活動の1つでは、私は断片的にGoogleマップを表示しています。 3gまたは高速インターネット接続で正常に動作しています。しかし、2gまたは非常に遅いインターネット接続では、私の携帯電話をハングアップしているし、しばらくしてから、私は '応答しない'メッセージを取得します。このような状況に対応できる方法はありますか?それは完全なコードではありません - :ここ コードAndroidの地図が2gまたは遅いインターネット接続で初期化されない

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     try { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_maps); 

      // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
      SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
        .findFragmentById(R.id.map); 
      mapFragment.getMapAsync(this); 

      InitializeClient(); 
      InitializeDB(); 


     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     mqtt=new MQTT(); 
     try{ 
      if(ip==null){ 
       System.out.println("Mqtt Initialized in Maps Activity"); 
       ip="demo.aiotm.in:1883"; 
      } 
      //mqtt.setHost("tcp://"+ip); 
      mqtt.setHost("tcp://10.30.60.242:1883"); 
      connection = mqtt.blockingConnection(); 
      connection.connect(); 
     }catch (Exception e){ 
      e.printStackTrace(); 

     } 

    } 

    private void InitializeClient() { 

     try { 
      //Initializing googleApiClient 
      googleApiClient = new GoogleApiClient.Builder(this) 
        .addConnectionCallbacks(this) 
        .addOnConnectionFailedListener(this) 
        .addApi(LocationServices.API) 
        .build(); 
      googleApiClient.connect(); 

     }catch (Exception e){e.printStackTrace();} 




     if (this.mMap != null) { 

      if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       // TODO: Consider calling 
       // ActivityCompat#requestPermissions 
       // here to request the missing permissions, and then overriding 
       // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
       //           int[] grantResults) 
       // to handle the case where the user grants the permission. See the documentation 
       // for ActivityCompat#requestPermissions for more details. 
       return; 
      }else {System.out.println(":::::::::::::::::::::::::::::::::: Map Not Initialized :::::::::::::::::::::::::::::::::::::::::;");} 
      mMap.setMyLocationEnabled(true); 


     } 
     if (googleApiClient.isConnected()||googleApiClient.isConnecting()){System.out.println("------------------------------------>Api is connecting<-----------------------------------");} 


    } 

上記のマップの活動のための私の初期化コードであるのです..!、私はこのくらいのコードは十分

NOTEあると思います。セキュリティ上の理由からコードの一部を削除しました。

答えて

0

getMapAsync()は、googleMapを受け取ったonMapReadyを提供します。

遅いインターネット接続の場合、マップを照会するには時間がかかる可能性がありますので、onMapReadyコールバックのgoogleApiClient初期化と権限リクエストを移動することができます。

あなたのコードは、この

@Override 
    public void onMapReady(GoogleMap googleMap) { 
    mGoogleMap = googleMap; 
    LatLngBounds SINGAPORE = new LatLngBounds(new LatLng(1.2729, 103.6066), new LatLng(1.3553, 104.0323)); 
    mGoogleMap.setLatLngBoundsForCameraTarget(SINGAPORE); 
    mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
    mGoogleMap.setPadding(0, 0, 16, 500); 

    //Initialize Google Play Services 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
      //Location Permission already granted 
      mGoogleMap.setMyLocationEnabled(true); 
     } else { 
      //Request Location Permission 
      checkLocationPermission(); 
     } 
    } else { 
     mGoogleMap.setMyLocationEnabled(true); 
    } 

    getGoogleApiClient(); 

    LocationRequest locationRequest = LocationRequest.create(); 
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest); 
    builder.setAlwaysShow(true); 
    final PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build()); 
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() { 
     @Override 
     public void onResult(@NonNull LocationSettingsResult locationSettingsResult) { 
      final Status status = locationSettingsResult.getStatus(); 
      final LocationSettingsStates state = locationSettingsResult.getLocationSettingsStates(); 
      switch (status.getStatusCode()) { 
       case LocationSettingsStatusCodes.SUCCESS: 
        //All location settings are satisfied 
        displayMarker(); 
        break; 
       case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 
        //Location settings are not satisfied, but could be fixed by showing a dialog 
        try { 
         //Show the dialog by calling startResolutionForResult(), and check the result in onActivityResult() 
         status.startResolutionForResult(getActivity(), FragmentSearchLocation.LOCATION_SETTINGS); 
        } catch (IntentSender.SendIntentException e) { 
         e.printStackTrace(); 
        } 
        break; 
       case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: 
        //Location settings are not satisfied. However, we have no way to fix the settings so we won't show the dialog 
        break; 
      } 
     } 
    }); 
} 
のようになります
関連する問題