I've seen this question in various places across Stack Overflow, however none of the answers are managing to solve my problem. The usual answer seems to tell the app to import android.support.v4.app.Fragment;
When I import this method it comes up that it's unused so I'm unsure whether I need it at all.
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
public class OpenStreetMap extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open_street_map);
OpenStreetMapFragment osmFragment = new OpenStreetMapFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction()
.add(R.id.osm_map, osmFragment)
.addToBackStack(null)
.commit();
}
}
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/osm_map">
<org.osmdroid.views.MapView android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
OpenStreetMapFragment
public class OpenStreetMapFragment extends FragmentActivity implements MapEventsReceiver, LocationListener, android.location.LocationListener, SensorEventListener {
static String mapQuestApiKey;
protected MapView map;
protected GeoPoint startPoint, destinationPoint;
protected ArrayList<GeoPoint> viaPoints;
protected Marker startMarker;
protected LocationManager mLocationManager;
protected DirectedLocationOverlay myLocationOverlay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapQuestApiKey = "xxxxxxxxxxxxxxxxxxxxxxx";
map = (MapView) findViewById(R.id.map);
map.setTileSource(TileSourceFactory.MAPNIK);
map.setBuiltInZoomControls(true);
map.setMultiTouchControls(true);
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
IMapController mapController = map.getController();
mapController.setZoom(12);
myLocationOverlay = new DirectedLocationOverlay(this);
map.getOverlays().add(myLocationOverlay);
if (savedInstanceState == null) {
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 = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
GeoPoint geoPoint = new GeoPoint(location);
mapController.setCenter(geoPoint);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0, this);
startPoint = null;
destinationPoint = null;
viaPoints = new ArrayList<GeoPoint>();
} else {
myLocationOverlay.setLocation((GeoPoint) savedInstanceState.getParcelable("location"));
//TODO: restore other aspects of myLocationOverlay...
startPoint = savedInstanceState.getParcelable("start");
destinationPoint = savedInstanceState.getParcelable("destination");
viaPoints = savedInstanceState.getParcelableArrayList("viapoints");
}
}
@Override public void onLocationChanged(final Location pLoc) {
GeoPoint newLocation = new GeoPoint(pLoc);
Log.e("Latitude", ":" + newLocation.getLatitude());
Log.e("Longitude", ":" + newLocation.getLongitude());
if (!myLocationOverlay.isEnabled()){
//we get the location for the first time:
myLocationOverlay.setEnabled(true);
map.getController().animateTo(newLocation);
}
myLocationOverlay.setLocation(newLocation);
String msg = "New Latitude: " + newLocation.getLatitude() + "New Longitude: " + newLocation.getLongitude();
myLocationOverlay.setAccuracy((int)pLoc.getAccuracy());
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
}