android-谷歌地图没有显示

时间:2017-09-27 07:04:55

标签: android google-maps

我在我的应用程序中使用谷歌地图有一个非常奇怪的问题,我已经构建了一个应用程序,它在我的手机中显示地图并且它没有问题,但它没有在另一部手机中显示,它有一个像这样的白色屏幕:

enter image description here

然后它显示了这个:

enter image description here

在同一部手机上,在另一个应用程序中显示地图没有问题。 你可以帮帮我吗 ?

这是我的代码:

public class Maps extends AppCompatActivity {
FetchCordinates fetchCordinates;
Intent locatorService = null;
MapView mMapView;
private GoogleMap googleMap;
Double lat = 0.0, lon = 0.0;
Typeface typeface;

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

    typeface = Func.getTypeFace(this);

    mMapView = (MapView) findViewById(R.id.mapView);
    mMapView.onCreate(savedInstanceState);
    mMapView.onResume(); // needed to get the map to display immediately

    mapBuilder();

    try {
        Bundle bl = getIntent().getExtras();
        if (bl != null) {
            lat =(bl.getDouble("lat"));
            lon = (bl.getDouble("lon"));

            mMapView.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(GoogleMap mMap) {
                    googleMap = mMap;
                    mMapView.setVisibility(View.VISIBLE);
                    // For dropping a marker at a point on the Map
                    LatLng sydney = new LatLng(lat, lon);
                    googleMap.addMarker(new MarkerOptions().position(sydney).title(""));
                    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(sydney, 17));
                    // For zooming automatically to the location of the marker
                    CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build();
                    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
                }
            });
        }
    } catch (Exception e) {

    }

}

private void mapBuilder() {
    try {
        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();
        }
    } catch (Exception e) {
        MyToast.makeText(Maps.this, e.getMessage());
        e.printStackTrace();
    }

    Button mapbutton=(Button)findViewById(R.id.mapbutton);
    mapbutton.setTypeface(typeface);
    mapbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SharedPreferences settings = getSharedPreferences("settings", MODE_PRIVATE);
            SharedPreferences.Editor pref= settings.edit();
            pref.putString("lat", lat+"");
            pref.putString("lon", lon+"");
            pref.commit();
            onBackPressed();
        }
    });
}

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;
        MyToast.makeText(Maps.this, "موقعیت شما با موفقیت ثبت شد");

        mMapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap mMap) {
                googleMap = mMap;
                mMapView.setVisibility(View.VISIBLE);
                // For dropping a marker at a point on the Map
                LatLng sydney = new LatLng(lat, lon);
                googleMap.addMarker(new MarkerOptions().position(sydney).title(""));
                googleMap.setMyLocationEnabled(true);
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(sydney, 22));
                // For zooming automatically to the location of the marker
                CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build();
                googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
            }
        });
    }

    @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() {
    try {
        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)) {
            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);
        }
    } catch (Exception e) {
        MyToast.makeText(Maps.this, e.getMessage());
    }
}

@Override
protected void onStart() {
    super.onStart();
    if (mMapView != null && mMapView.getVisibility() == View.VISIBLE)
        mapBuilder();
}
你可以帮帮我吗?

0 个答案:

没有答案