2017-10-12 90 views
0

我測試過的很多手機的我的應用程序,它工作正常,並正確地顯示在地圖上,但它不會在某些設備上顯示。非常奇怪的是,沒有顯示地圖的設備在使用地圖的其他應用中效果不錯。我的意思是,設備會顯示在地圖和其他應用程序工作正常/Android的 - 谷歌地圖沒有顯示在某些設備

這是我的代碼:

public class Maps extends AppCompatActivity { 
    MapFragment googleMap; 
    GoogleMap Map; 
    Intent locatorService = null; 
    Double lat = 0.0, lon = 0.0; 
    Typeface typeface; 
    int isAvailable = 777; 
    private LatLng location; 
    Boolean forEdit = false; 

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

     isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
     typeface = Func.getTypeFace(this); 

     final Bundle bl = getIntent().getExtras(); 
     if (bl != null) { 
      forEdit = true; 
     } 

     if (isAvailable == 0) { 
      googleMap = (MapFragment) getFragmentManager().findFragmentById(R.id.map); 
      googleMap.getMapAsync(new OnMapReadyCallback() { 
       @Override 
       public void onMapReady(GoogleMap googleMap) { 
        Map = googleMap; 

        if (googleMap == null) { 
         Toast.makeText(getApplicationContext(), 
           "نقشه روی گوشی شما قابل نمایش نیست", Toast.LENGTH_SHORT) 
           .show(); 
        } 

       } 
      }); 
     } 
     mapBuilder(); 
    } 

    private void mapBuilder() { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M 
       && checkSelfPermission(
       android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED 
       && checkSelfPermission(
       android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      ActivityCompat.requestPermissions(Maps.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1); 
     } else { 
      doGPS(); 
     } 


    } 

    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
     switch (requestCode) { 
      case 1: { 
       // If request is cancelled, the result arrays are empty. 
       if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        mapBuilder(); 
       } else { 
        MyToast.makeText(Maps.this, "دسترسی به جی پی اس غیرفعال است"); 
       } 
       return; 
      } 
     } 
    } 

    public boolean stopService() { 
     if (this.locatorService != null) { 
      this.locatorService = null; 
     } 
     return true; 
    } 

    public boolean startService() { 
     try { 
      FetchCordinates fetchCordinates = new FetchCordinates(); 
      fetchCordinates.execute(); 
      return true; 
     } catch (Exception error) { 
      return false; 
     } 
    } 

    public AlertDialog CreateAlert(String title, String message) { 
     AlertDialog alert = new AlertDialog.Builder(this).create(); 

     alert.setTitle(title); 

     alert.setMessage(message); 

     return alert; 

    } 

    public class FetchCordinates extends AsyncTask<String, Integer, String> { 
     AlertDialog.Builder a; 
     AlertDialog dialog; 

     public double lati = 0.0; 
     public double longi = 0.0; 

     public LocationManager mLocationManager; 
     public VeggsterLocationListener mVeggsterLocationListener; 

     @Override 
     protected void onPreExecute() { 
      mVeggsterLocationListener = new VeggsterLocationListener(); 
      mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

      mLocationManager.requestLocationUpdates(
        LocationManager.NETWORK_PROVIDER, 0, 0, 
        mVeggsterLocationListener); 
     } 

     @Override 
     protected void onCancelled() { 
      System.out.println("Cancelled by user!"); 
      dialog.dismiss(); 
      mLocationManager.removeUpdates(mVeggsterLocationListener); 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      try { 
       if (dialog != null) 
        dialog.dismiss(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      lat = lati; 
      lon = longi; 

      location = new LatLng(lat, lon); 
      Map.addMarker(new MarkerOptions().position(location).title("مکان فعلی")); 
      CameraUpdate update = CameraUpdateFactory.newLatLng(location); 
      update = CameraUpdateFactory.newLatLngZoom(location, 15); //zooom 
      Map.animateCamera(update); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      // TODO Auto-generated method stub 

      while (this.lati == 0.0) { 

      } 
      return null; 
     } 

     public class VeggsterLocationListener implements LocationListener { 

      @Override 
      public void onLocationChanged(Location location) { 

       int lat = (int) location.getLatitude(); // * 1E6); 
       int log = (int) location.getLongitude(); // * 1E6); 
       int acc = (int) (location.getAccuracy()); 

       String info = location.getProvider(); 
       try { 

        // LocatorService.myLatitude=location.getLatitude(); 

        // LocatorService.myLongitude=location.getLongitude(); 

        lati = location.getLatitude(); 
        longi = location.getLongitude(); 

       } catch (Exception e) { 
        // progDailog.dismiss(); 
        // Toast.makeText(getApplicationContext(),"Unable to get Location" 
        // , Toast.LENGTH_LONG).show(); 
       } 

      } 

      @Override 
      public void onProviderDisabled(String provider) { 
       Log.i("OnProviderDisabled", "OnProviderDisabled"); 
      } 

      @Override 
      public void onProviderEnabled(String provider) { 
       Log.i("onProviderEnabled", "onProviderEnabled"); 
      } 

      @Override 
      public void onStatusChanged(String provider, int status, 
             Bundle extras) { 
       Log.i("onStatusChanged", "onStatusChanged"); 

      } 

     } 

    } 

    private void doGPS() { 
     LocationManager mlocManager = null; 
     LocationListener mlocListener; 
     mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     mlocListener = new MyLocationListener(); 
     mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 
     if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && forEdit == false) { 
      startService(); 
     } else { 
      android.support.v7.app.AlertDialog.Builder a = new android.support.v7.app.AlertDialog.Builder(Maps.this); 
      a.setMessage(("جی پی اس خاموش است. آیا میخواهید روشن کنید؟")); 
      a.setPositiveButton(("بله"), new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.cancel(); 
        LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); 
        boolean enabled = service 
          .isProviderEnabled(LocationManager.GPS_PROVIDER); 
        if (!enabled) { 
         Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
         startActivity(intent); 
        } 
       } 
      }); 
      a.setNegativeButton(("خیر"), new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.cancel(); 
       } 
      }); 

      android.support.v7.app.AlertDialog dialog = a.show(); 
      TextView messageText = (TextView) dialog.findViewById(android.R.id.message); 
      messageText.setGravity(Gravity.RIGHT); 
      messageText.setTypeface(typeface); 
     } 

    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     if (Map != null && isAvailable != 777) 
      mapBuilder(); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     stopService(); 
    } 

} 

這是XML佈局:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <fragment 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.MapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</FrameLayout> 

你能幫助我嗎?我的代碼有什麼問題?

+0

該裝置它不工作? api和谷歌播放服務版本。 READ_GSERVICES也授予此權限嗎? – tan

回答

0

檢查isGooglePlayServicesAvailable的響應,有一個很好的機會得到「SERVICE_VERSION_UPDATE_REQUIRED」。在這種情況下,用戶必須更新Google Play服務。

谷歌播放服務將會有其他谷歌應用程序,如Google,谷歌地圖,谷歌鐸等得到更新獲取自動更新。

switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity())) { 
     case ConnectionResult.SUCCESS: 
      googleMap = (MapFragment) getFragmentManager().findFragmentById(R.id.map); 
      googleMap.getMapAsync(new OnMapReadyCallback() { 
      @Override 
      public void onMapReady(GoogleMap googleMap) { 
       map = googleMap; 
       } 
      }); 
      break; 
     case ConnectionResult.SERVICE_MISSING: 
      Toast.makeText(getActivity(), "Google Play Services Missing", Toast.LENGTH_SHORT).show(); 
      break; 
     case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED: 
      //The user has to update play services 
      Toast.makeText(getActivity(), "Update Google Play Services", Toast.LENGTH_SHORT).show(); 
      break; 
     default: 
      Toast.makeText(getActivity(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()), Toast.LENGTH_SHORT).show(); 
    } 
相關問題