我试图找到我的位置。 它正在工作,但我的本地化是假的!!
这是班级:
public class maaaap extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
String[] g, s;
double d1, d2, lat, A, latitude, longitude;
LatLng l;
LocationManager locationManager;
GPSTracker gpsTracker;
LatLng tunis;
Location mLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maaaap);
// 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);
// gpsTracker= new GPSTracker(getApplicationContext());
//mLocation=gpsTracker.getLocation();
// if(mLocation!=null){
// latitude= mLocation.getLatitude();
// longitude= mLocation.getLongitude();
}//}
public void table() {
g = new String[]{"36.827831", "36.836143", "36.832123"};
s = new String[]{"10.144411", "10.143982", "10.150763"};
//for (int i = 0;i <g.length; i++) {
// la.add(g[i].toString());
//lo.add(s[i].toString());
// }
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
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);
googleMap.getUiSettings().setCompassEnabled(false);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
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;
}
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location!=null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
}
table();
for (int i = 0; i < 3; i++) {
d1 = Double.valueOf(g[i]);
d2 = Double.valueOf(s[i]);
mMap.addMarker(new MarkerOptions().position(new LatLng(d1, d2)).title("kolha touness"));
}
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title(" touness"));
}
}