1

I am trying to implement OpenStreetMap using osmdroid. I successfully got the world map set to a particular set of geo co-ordinates. here is my code:

 package com.example.re.osm;
     //OSM MAP CODE
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.preference.PreferenceManager;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.RelativeLayout;

import org.osmdroid.api.IMapController;
import org.osmdroid.config.Configuration;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider;
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay;

public class MainActivity extends Activity {

private MapView mMapView;
private MyLocationNewOverlay locationOverlay;

@Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    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_main);

    MapView map = (MapView) findViewById(R.id.map);
    map.setTileSource(TileSourceFactory.MAPNIK);
    map.setBuiltInZoomControls(true);
    map.setMultiTouchControls(true);

    /*IMapController mapController = map.getController();
    mapController.setZoom(9);
    GeoPoint startPoint = new GeoPoint(48.8583, 2.2944);
    mapController.setCenter(startPoint);*/
    //add
    locationOverlay = new MyLocationNewOverlay(map);
    mOsmOverlays.add(locationOverlay);
}

public void onResume(){
    super.onResume();
    //this will refresh the osmdroid configuration on resuming.
    //if you make changes to the configuration, use
    //SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    //Configuration.getInstance().save(this, prefs);
    Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this));
    //add
    locationOverlay.enableMyLocation();
}

public void onPause(){
    super.onPause();
    //add
    locationOverlay.disableMyLocation();
}

}

The current version of osmdroid (5.6.5) is used.

Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.re.osm"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="OSM" >
        <activity
            android:name=".MainActivity"
            android:label="OSM" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

How do I get the current user location in this?. Should I also use resource proxy?

Thanks.

Thomas
  • 121
  • 2
  • 12
  • Do you just want to display current position of the user? I that case it's already answered on stackoverflow https://stackoverflow.com/questions/8804788/how-to-display-the-current-position-pointer-in-osmdroid – Josef Adamcik Aug 29 '17 at 11:06
  • @josef In which part of my code should I add this? I tried this example but I am not able to get the current user location. I am using osmdroid 5.6.5. Custom resource proxy applies only to versions prior to 5.2 Can you please show me a small demo sir. – Thomas Aug 29 '17 at 11:10
  • Are there any errors? Does your application handle permissions for a location? You'll have to declare permissioinss in manifest and handle the permission request if you are building against android 6 or newer. Check this question: https://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android – Josef Adamcik Aug 29 '17 at 11:16
  • I edited my question and added the manifest file also. if I use the above code I am getting errors and red marking all over. I am confused in which part of the code should I implement code for the current user loc? Should I implement resource proxy class too? – Thomas Aug 29 '17 at 11:21
  • what kind of errors do you get? – Michael Meyer Aug 29 '17 at 11:23
  • Cannot resolve symbol MyLocationOverlay, cannot resolve symbol mOsmvController – Thomas Aug 29 '17 at 11:27
  • It seems the answer is outdated and MyLocationOverlay does not exist anymore (or was renamed). – Josef Adamcik Aug 29 '17 at 11:36
  • @josef.adamcik So how should I get the current user location? – Thomas Aug 29 '17 at 11:37
  • I wrote a proper response. – Josef Adamcik Aug 29 '17 at 11:45

1 Answers1

0

You can use MyLocationNewOverlay

...
GeoPoint startPoint = new GeoPoint(48.8583, 2.2944);
mapController.setCenter(startPoint);
//add
GpsMyLocationProvider provider = new GpsMyLocationProvider(this);
provider.addLocationSource(LocationManager.NETWORK_PROVIDER);
locationOverlay = new MyLocationNewOverlay(map, provider);
locationOverlay.enableFollowLocation();
locationOverlay.runOnFirstFix(new Runnable() {
    public void run() {
        Log.d("MyTag", String.format("First location fix: %s", locationOverlay.getLastFix()));
    }
});
map.getOverlayManager().add(locationOverlay);

In onResume method in the activity then add:

public void onResume(){
    super.onResume();
    //this will refresh the osmdroid configuration on resuming.
    //if you make changes to the configuration, use
    //SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    //Configuration.getInstance().save(this, prefs);
    Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this));
    //add
    locationOverlay.enableMyLocation();
}

And in onPause

public void onPause(){
    super.onResume();
    //add
    locationOverlay.disableMyLocation();
}

Where locationOverlay is obviously a field typed as MyLocationNewOverlay.

For more details check documentation for the class. There's also an example in the sample application.

Josef Adamcik
  • 5,620
  • 3
  • 36
  • 42
  • @ i added this code and also updated the code in the question. I get this error : cannot resolve symbol mOsmOverlays – Thomas Aug 29 '17 at 11:53
  • Ok, becase theres no variable of that name. Change it to map.getOverlays().add(..) – Josef Adamcik Aug 29 '17 at 11:55
  • The error went away. But I am not able to see my current location in the map. What should I change? – Thomas Aug 29 '17 at 12:08
  • I need current user location in the center of the screen – Thomas Aug 29 '17 at 12:20
  • Ok, I checked the sample application https://github.com/osmdroid/osmdroid/blob/f2394625e6f81371cc3f1db2ff71e9abd53c32e1/OpenStreetMapViewer/src/main/java/org/osmdroid/samplefragments/location/SampleMyLocationWithClick.java and modified response a little bit so it's the same as the sample. Are you sure your app is able to get the location from the system? – Josef Adamcik Aug 29 '17 at 12:25
  • Also I added a debug log on first location fix. You should check logcat if "First location fix" is displayed. – Josef Adamcik Aug 29 '17 at 12:29
  • Yes, with the original question which I posted with the geocoordinates fixed, I got paris at the centre of the map. Now I need my current location, for example If I am in Berlin, the I must get my current location at the centre of the map – Thomas Aug 29 '17 at 12:30
  • @Thomas Well if you are in Berlin and your phone shows Paris it actually doesn't work, does it? – Josef Adamcik Aug 29 '17 at 12:34
  • It shows paris when i fix the geo cordinates. But when i dont fix it, I cannot see any location – Thomas Aug 29 '17 at 12:37
  • Please help. I am really stuck in this problem. – Thomas Aug 29 '17 at 12:44
  • Ok. But when you set the location manually It doesn't say anything about your access to the location of the device. Try the latest changes and check the logcat for "First location fix..." as I wrote above. If it appears your app got location. If it does not appera you may have problem on other level not related to osmdroid. – Josef Adamcik Aug 29 '17 at 12:45
  • First location fix is not displayed in the logcat – Thomas Aug 29 '17 at 12:53
  • Ok, next thing: try to create MyLocationNewOverlay with custom instance of gpslocationprovider (i updated the example): GpsMyLocationProvider provider = new GpsMyLocationProvider(this); provider.addLocationSource(LocationManager.NETWORK_PROVIDER); locationOverlay = new MyLocationNewOverlay(map, provider); – Josef Adamcik Aug 29 '17 at 13:06
  • Man, you should try to debug simple problems yourself, you are the one who has running android studio with your code and all the tools and hints. Try to switch the arguments, or check the documentation. This is not free codewritng service and I am trying to help you as I can. But this is starting to be ridiculous. – Josef Adamcik Aug 29 '17 at 13:21
  • Yes, I fixed the problems and the errors too. I will check logcat and try to get the location. – Thomas Aug 29 '17 at 13:24