我使用此代码在地图上显示我的位置,但地图在应用中不可见。应用运行正常。我不知道自己做错了什么。请帮帮我。谢谢。
public class MapsActivity extends FragmentActivity implements LocationListener,OnMapReadyCallback{
GoogleMap mMap;
LatLng myPosition;
Location location;
LocationManager locationManager;
double latitude = 0, longitude = 0;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
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);
// mMap = mapFragment.getMap();
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap mMap) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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;
}
mMap.setMyLocationEnabled(true);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
// getting GPS status
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
AlertDialog.Builder alertDialog=new AlertDialog.Builder(this);
alertDialog.setTitle("GPS Is Off");
alertDialog.setMessage("Please Enable GPS for better Location");
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
}
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
{
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 100f, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 100f, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
/* String provider = locationManager.getBestProvider(criteria, true).toString();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 100.0f, this);
// locationManager.requestLocationUpdates(provider, 1000, 10f, this);
location = locationManager.getLastKnownLocation(provider);*/
//Toast.makeText(getApplicationContext(),provider,Toast.LENGTH_LONG).show();
if (location != null) {
onLocationChanged(location);
}
// latitude = location.getLatitude();
// longitude = location.getLongitude();
myPosition = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(myPosition));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 16));
Timer t = new Timer(false); //To show text for required time
t.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
dialogbox();
}
});
}
}, 10000);
}
}
还有mMap.getMyType给出' 1'值表示法线贴图。但地图不可见。此外,我将纬度和经度值传递给另一个活动。在该活动中,它将打印所提供的纬度和经度的地址。这部分工作正常,这意味着应用程序正确定位我的位置,但地图不可见。