Answer the question
In order to leave comments, you need to log in
location android?
public class MapsActivity extends AppCompatActivity implements LocationListener {
GoogleMap map;
double lon = 0;
double lat = 0;
LocationManager lm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
locationWork();
createMap();
createMap();
}
private void locationWork() {
lm = (LocationManager) getSystemService(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) {
Toast.makeText(MapsActivity.this, "Включите GPS и откройте интернет подключение", Toast.LENGTH_SHORT).show();
return;
}
lm.requestLocationUpdates(lm.NETWORK_PROVIDER,0,0,this);
}
private void createMap() {
map = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
if (map == null) {
Toast.makeText(this, "NULL MAP!", Toast.LENGTH_SHORT).show();
}
}
private void addMarker(double lon, double lat){
map.addMarker(new MarkerOptions()
.position(new LatLng(lon, lat))
.title("Hello world"));
}
@Override
public void onLocationChanged(Location location) {
lon = location.getLongitude();
lat = location.getLatitude();
addMarker(lon,lat);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question