Finding the coordinates of the initial location in OSMDROID - android

I am using osmdroid and i am just having an issue with getting the coordinates of my initial location on the map.
I can get the location on the map and i can get the initial location to print inside the runable.
Is there any way i can get just the initial coordinates? I dont need to update them or anything..
I just need them so i can design a route between the initial location and the destination....
Any help would be great thanks!
Here is my code;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Context ctx = getApplicationContext();
//important! set your user agent to prevent getting banned from the osm servers
Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
setContentView(R.layout.activity_map);
mMapView = (MapView) findViewById(R.id.mapview);
mMapView.setTileSource(TileSourceFactory.MAPNIK);
mMapView.setBuiltInZoomControls(true);
mMapView.setMultiTouchControls(true);
IMapController mapController = mMapView.getController();
mapController.setZoom(13);
GeoPoint startPoint = new GeoPoint(52.167247, -6.888583);
Marker POI1 = new Marker(mMapView);
Marker POI2 = new Marker(mMapView);
Marker POI3 = new Marker(mMapView);
Marker POI4 = new Marker(mMapView);
POI1.setPosition(new GeoPoint(52.187668, -6.866953));
POI2.setPosition(new GeoPoint(52.124359, -6.926442));
POI3.setPosition(new GeoPoint(52.222310, -6.933613));
POI4.setPosition(new GeoPoint(52.221245, -6.830961));
List<OverlayItem> markers = null;
mMapView.getOverlays().add(POI1);
mMapView.getOverlays().add(POI2);
mMapView.getOverlays().add(POI3);
mMapView.getOverlays().add(POI4);
mapController.setCenter(startPoint);
//add
GpsMyLocationProvider provider = new GpsMyLocationProvider(this);
provider.addLocationSource(LocationManager.NETWORK_PROVIDER);
locationOverlay = new MyLocationNewOverlay(provider, mMapView);
locationOverlay.enableMyLocation();
locationOverlay.enableFollowLocation();
locationOverlay.runOnFirstFix(new Runnable() {
public void run() {
Log.d("MyTag", String.format("First location fix: %s", locationOverlay.getLastFix()));
}
});
mMapView.getOverlayManager().add(locationOverlay);
//System.out.println("hello joe: " + currentLocation.getLongitude() );
RoadManager roadManager = new OSRMRoadManager(this);
ArrayList<GeoPoint> wayPoints = new ArrayList<GeoPoint>();
wayPoints.add(startPoint);
GeoPoint endPoint = new GeoPoint(52.221245, -6.830961);
wayPoints.add(endPoint);
Road road = roadManager.getRoad(wayPoints);
if (road.mStatus != Road.STATUS_OK){
Toast.makeText(this, "Error when loading road", Toast.LENGTH_LONG).show();
}
Polyline roadOverlay = RoadManager.buildRoadOverlay(road);
mMapView.getOverlays().add(roadOverlay);
}

I solved this by using the location provider

Related

Drag polyline along with marker in openstreetmap?

I'm currently developing an android application that would allow users to draw Polylines with Markers or point in the polyline when user long press on points how to dragline with points move also line would be move on the map. how do I achieve this I drag marker but cant move marker
public class MainMapActivity extends AppCompatActivity {
GeoPoint startPoint;
MapView map;
Road road;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_map);
map = (MapView) findViewById(R.id.map);
map.setTileSource(TileSourceFactory.MAPNIK);
map.setBuiltInZoomControls(true);
map.setMultiTouchControls(true);
GpsTracking gps=new GpsTracking(MainMapActivity.this);
if (gps.canGetLocation()) {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
startPoint = new GeoPoint(latitude, longitude);
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
} else {
gps.showSettingsAlert();
}
// GeoPoint startPoint = new GeoPoint(48.13, -1.63);
IMapController mapController = map.getController();
mapController.setZoom(17);
mapController.setCenter(startPoint);
Marker startMarker = new Marker(map);
startMarker.setPosition(startPoint);
startMarker.setDraggable(true);
startMarker.setOnMarkerDragListener(new OnMarkerDragListenerDrawer());
startMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
map.getOverlays().add(startMarker);
//Set-up your start and end points:
RoadManager roadManager = new OSRMRoadManager(this);
ArrayList<GeoPoint> waypoints = new ArrayList<GeoPoint>();
waypoints.add(startPoint);
GeoPoint endPoint = new GeoPoint(31.382108, 74.260107);
waypoints.add(endPoint);
// retreive the road between those points:
Road road = roadManager.getRoad(waypoints);
// build a Polyline with the route shape:
Polyline polyline=new Polyline();
polyline.setOnClickListener(new Polyline.OnClickListener() {
#Override
public boolean onClick(Polyline polyline, MapView mapView, GeoPoint eventPos) {
return false;
}
});
Polyline roadOverlay = RoadManager.buildRoadOverlay(road);
//Polyline to the overlays of your map:
map.getOverlays().add(roadOverlay);
//Refresh the map!
map.invalidate();
//3. Showing the Route steps on the map
FolderOverlay roadMarkers = new FolderOverlay();
map.getOverlays().add(roadMarkers);
Drawable nodeIcon = ResourcesCompat.getDrawable(getResources(), R.drawable.marker_node, null);
for (int i = 0; i < road.mNodes.size(); i++) {
RoadNode node = road.mNodes.get(i);
Marker nodeMarker = new Marker(map);
nodeMarker.setDraggable(true);
nodeMarker.setOnMarkerDragListener(new OnMarkerDragListenerDrawer());
nodeMarker.setPosition(node.mLocation);
nodeMarker.setIcon(nodeIcon);
//4. Filling the bubbles
nodeMarker.setTitle("Step " + i);
nodeMarker.setSnippet(node.mInstructions);
nodeMarker.setSubDescription(Road.getLengthDurationText(this, node.mLength, node.mDuration));
Drawable iconContinue = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_continue, null);
nodeMarker.setImage(iconContinue);
//4. end
roadMarkers.add(nodeMarker);
}
}
class OnMarkerDragListenerDrawer implements Marker.OnMarkerDragListener {
ArrayList<GeoPoint> mTrace;
Polyline mPolyline;
OnMarkerDragListenerDrawer() {
mTrace = new ArrayList<GeoPoint>(100);
mPolyline = new Polyline();
mPolyline.setColor(0xAA0000FF);
mPolyline.setWidth(2.0f);
mPolyline.setGeodesic(true);
map.getOverlays().add(mPolyline);
}
#Override public void onMarkerDrag(Marker marker) {
//mTrace.add(marker.getPosition());
}
#Override public void onMarkerDragEnd(Marker marker) {
mTrace.add(marker.getPosition());
mPolyline.setPoints(mTrace);
map.invalidate();
}
#Override public void onMarkerDragStart(Marker marker) {
//mTrace.add(marker.getPosition());
}
}
}
Does anyone know how I can achieve this? above is a snippet of my codes. Thanks!

Take screenshot of the Displayed OpenStreetMap

I made an independent app to show the current user location using OpenStreetMaps. This is my Main Activity:
I used osmdroid 5.6.2
package com.example.re.osm;
public class MainActivity extends Activity {
private LocationManager locationManager;
private Location onlyOneLocation;
private final int REQUEST_FINE_LOCATION = 1234;
public GeoPoint startPoint;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context ctx = getApplicationContext();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_FINE_LOCATION);
Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
setContentView(R.layout.activity_main);
final MapView map = (MapView) findViewById(R.id.map);
map.setTileSource(TileSourceFactory.MAPNIK);
map.setBuiltInZoomControls(true);
map.setMultiTouchControls(true);
final IMapController mapController = map.getController();
mapController.setZoom(18);
CurrentLocation.requestSingleUpdate(this,
new CurrentLocation.LocationCallback() {
#Override public void onNewLocationAvailable(GPSCoordinates location) {
Log.d("Location", "my location is " + location.latitude + " : " + location.longitude);
startPoint = new GeoPoint(location.latitude, location.longitude);
// My Location Overlay
Marker marker=new Marker(map);
marker.setPosition(startPoint);
marker.setTitle("Your Location");
marker.showInfoWindow();
map.getOverlays().add(marker);
map.invalidate();
//This geopoint is currently static and shows a single location everytime the app is executed
mapController.setCenter(startPoint);
}
});
}
}
I need to take the screenshot of the map and display it. Any idea of how should I do it?
Thanks

Android MapView (google maps) v1 focus by Locale or country name

when you go to Google maps in the browser you can type a country name and the map focuses on that country. Is it possible to get this on a MapView? I'd like my MapView to initially focus on user's country if a location is not available.
EDIT: here's what I have so far:
private MapView mvClear;
private MyLocationOverlay compass;
private MapController controller;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clear_map);
mvClear = (MapView) findViewById(R.id.mvClear);
mvClear.setBuiltInZoomControls(true);
TouchOverlay touchOverlay = new TouchOverlay();
List<Overlay> overlays = mvClear.getOverlays();
overlays.add(touchOverlay);
compass = new MyLocationOverlay(this, mvClear);
overlays.add(compass);
controller = mvClear.getController();
controller.setZoom(8);
Geocoder coder = new Geocoder(this);
try {
List<Address> address = coder.getFromLocationName(Locale.getDefault().getCountry(), 1);
int lat = (int) address.get(0).getLatitude();
int lon = (int) address.get(0).getLongitude();
GeoPoint point = new GeoPoint(lat, lon);
controller.setCenter(point);
} catch (IOException e) {
e.printStackTrace();
}
}
No focus.
Best solution I found was to save the default center point as String resource so it can be "translated" if the app is localized. That's it for now..

Basic google map api - adding an overlay question (tag on a GeoPoint)

I have the following code ...
HERE the variable point is a Geopoint object .
I need the current GPS location of my Android , to be updated on this point...
What do I call ? to define the current location on POINT ?!
// create a map view
RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.mainlayout);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(true);
mapController = mapView.getController();
mapController.setZoom(14); // Zoom 1 is world view
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, new GeoUpdateHandler());
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);
OverlayItem overlayitem = new OverlayItem(point, "Hola, people!", "I am on Earth");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
public class GeoUpdateHandler implements LocationListener {
#Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(lat, lng);
mapController.animateTo(point);
//mapController.setCenter(point);
}
Take the look the following class
http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/MyLocationOverlay.html
You could just use this class to display user's current location on the map.

MyLocationOverlay: getMyLocation returning null

I have a mapview page that displays the current location in the map.
I am using MyLocationOverlay for the purpose. The code for that goes as follows:
myLocationOverlay = new MyLocationOverlay(this, mapView);
myLocationOverlay.enableCompass();
myLocationOverlay.enableMyLocation();
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
geopoint = myLocationOverlay.getMyLocation();
ItemizedOverlay itemizedoverlay = new ItemizedOverlay(drawable);
try{
overlayitem = new OverlayItem(geopoint, "", "");
}
catch(Exception e){
e.printStackTrace();
}
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
mapController.animateTo(geoPoint);
}
});
The above code works and gets the current location through gps and sets a marker at that point.
But the code inside try block is the issue.
I am getting null value inside geopoint.
Actually I have to get that point and do some calculations with distance and all. For that I have to get the correct value inside that geopoint.
Can anyone please tell the solution for this?
I've been working on the same problem for a day now and here's what I've discovered. For some reason this works
LocationManager lm;
GeoPoint gp;
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location lastKnownLoc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (lastKnownLoc != null){
int longTemp = (int)(lastKnownLoc.getLongitude()* 1000000);
int latTemp = (int)(lastKnownLoc.getLatitude() * 1000000);
gp = new GeoPoint(latTemp, longTemp);
}
and this doesn't
gp = myLocOverlay.getMyLocation();
Tracking current location can be done by using MyLocationOverlay class and through LocationManager. Using MyLocationOverlay, the code below works..
MyLocationOverlay myLocationOverlay;
MapView mapView;
MapController mapcontroller;
OverlayItem overlayitem;
List<Overlay> mapoverlays;
GeoPoint geopoint;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.currentlocation);
mapView = (MapView) findViewById(R.id.map);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
mapView.setStreetView(true);
mapView.setTraffic(true);
mapcontroller = mapView.getController();
mapoverlays = mapView.getOverlays();
drawable = this.getResources().getDrawable(R.drawable.ic_map_red);
mapView.invalidate();
getCurrentLocation();
}
public void getCurrentLocation(){
myLocationOverlay = new MyLocationOverlay(this, mapView);
myLocationOverlay.enableCompass();
myLocationOverlay.enableMyLocation();
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
ItemizedOverlay itemizedoverlay = new ItemizedOverlay(drawable);
try {
latitude = myLocationOverlay.getMyLocation().getLatitudeE6();
longitude = myLocationOverlay.getMyLocation().getLongitudeE6();
geopoint = new GeoPoint((int) (latitude), (int) (longitude));
overlayitem = new OverlayItem(geopoint, "", "");
itemizedoverlay.addOverlay(overlayitem);
mapoverlays.add(itemizedoverlay);
mapcontroller.animateTo(geopoint);
mapcontroller.setZoom(16);
}
catch (Exception e) {}
}
});
}
If you override:
onLocationChanged();
Remember to call:
super.onLocationChanged();
after or before your own code. This appears to ensure that the variable
getMyLocation()
returns is not null.
The friend upstairs made me rise that try override nothing, neither draw nor drawMyLocation.
Just like this:
MyLocationOverlay myLocationOverlay = new SelfLocationOverlay(this, mapView);
myLocationOverlay.enableMyLocation();
myLocationOverlay.disableCompass();
mapView.getOverlays().add(myLocationOverlay);

Categories

Resources