How to using OpenStreetMap library on GitHub [closed] - android

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am a beginner in Android, and I need to import OpenStreetMap in my application. I have not found a way to do it. This is the library that I need: link
The library has a tutorial, but I couldn't do it yet.

First of all go to: https://github.com/osmdroid/osmdroid
Here's an official repository of OpenStreetMap for Android. Add this to bookmark, as you would find nice wiki documentation and similar issues to future yours.
Pre-requirements
Go to your build.gradle file and add these dependencies:
repositories {
mavenCentral()
}
dependencies {
compile 'org.osmdroid:osmdroid-android:5.4.1:release#aar'
}
You can always download it manually from here and add to assets folder of your project.
PROTIP: Set your targetSdkVersion to 22 for now to avoid problems
with runtime permissions on Android version 6.0 and higher.
Go to your AndroidManifest.xml and add:
<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" />
<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" />
Now create your layout for your fisrst app
Hello world example
Create a "src/main/res/layouts/main.xml" layout like this one. With Android Studio, it probably created one already called. The default is "src/main/res/layouts/activity_main.xml":
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<org.osmdroid.views.MapView android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Create or open existing the main activity (MainActivity.java):
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.views.MapView;
public class MainActivity extends Activity {
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Important! Set your user agent to prevent getting banned from the OSM servers
org.osmdroid.tileprovider.constants.OpenStreetMapTileProviderConstants.setUserAgentValue(BuildConfig.APPLICATION_ID);
MapView map = (MapView) findViewById(R.id.map);
map.setTileSource(TileSourceFactory.MAPNIK);
}
}
And that's enough to give it a try and see the world map.
Add default zoom buttons and ability to zoom with 2 fingers (multi-touch):
map.setBuiltInZoomControls(true);
map.setMultiTouchControls(true);
To move the map on a default view point, you need access to the map controller:
IMapController mapController = map.getController();
mapController.setZoom(9);
GeoPoint startPoint = new GeoPoint(48.8583, 2.2944);
mapController.setCenter(startPoint);
That's it. If you wish more, check: https://github.com/osmdroid/osmdroid/wiki/How-to-use-the-osmdroid-library#advanced-tutorial
I hope it will help.

Related

Adding a Gps Layer to my android application using arcgis runtime sdk in android studio

I am trying to add a GPS layer to my map in my app.
the problem is that am not getting a good result from the code. Am getting a red highlights on some of the codes.
This is code below.
// create the tiled layer and add to map (could also be an ArcGISLocalTiledLayer)
ArcGISTiledLayer tiledLayer = new ArcGISTiledLayer(
"http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
map.getLayers().add(tiledLayer);
// create a GPS layer and add to map
GPSLayer gpsLayer = new GPSLayer();
map.getLayers().add(gpsLayer);
Use LocationDisplay instead of GPSLayer
// get the LocationDisplay from MapView
LocationDisplay mLocationDisplay = mMapView.getLocationDisplay();
mLocationDisplay.start();
Don't forget to add these lines in your manifest file:
<manifest ... >
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>
Look at samples for more details:
https://github.com/Esri/arcgis-runtime-samples-android/tree/master/java/display-device-location

OSMdroid Cannot get current location in android studio

Firstly I am new to OSMdroid.
I know there is other similar questions on this topic but there are no recent questions and the answers to the older questions don't work on my code...
I can't get the current location using OSMDroid.
I have used the simple tutorial given on github and the suggestions given in previous questions.
I have the latest version of OSMDroid but when I try to add a tag for the current location, nothing comes up.
I don't even get errors, the map just displays with no marker for the current location. Help! Here is my code.
Any help would be greatly appreciated as it is very annoying!
Java Class
#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_map);
mMapView = (MapView) findViewById(R.id.mapview);
mMapView.setTileSource(TileSourceFactory.MAPNIK);
mMapView.setBuiltInZoomControls(true);
mMapView.setMultiTouchControls(true);
IMapController mapController = mMapView.getController();
mapController.setZoom(9);
GeoPoint startPoint = new GeoPoint(52.1237453, 6.9293683);
mapController.setCenter(startPoint);
this.mLocationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(ctx),mMapView);
this.mLocationOverlay.enableMyLocation();
mMapView.getOverlays().add(this.mLocationOverlay);
XML code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<org.osmdroid.views.MapView
android:id="#+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tilesource="Mapnik" />
</LinearLayout>
Two possible reasons.
1) Your manifest is missing permissions for ACCESS_FINE_LOCATION
2) If the target SDK is 23 or newer, you have to ask for user level GPS permissions before creating the GpsMyLocationProvider
There are examples of both in the osmdroid sample application's source code, located here:
https://github.com/osmdroid/osmdroid/blob/master/OpenStreetMapViewer/src/main/java/org/osmdroid/intro/PermissionsFragment.java#L94
and here:
https://github.com/osmdroid/osmdroid/blob/master/OpenStreetMapViewer/src/main/AndroidManifest.xml#L24

android api with google map displays grid

I am creating an android application that uses google maps api v2.
I believe I have done everything as google tutorial sais but still I am getting only the google maps grid (no map). I have created a debug key from using SHA1 of keystore.debug.
below are my settings:
Eclipse java compiler version 1.7
Project java compiler version 1.6
application manifest:
<manifest ...>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="my.app.test.permission.MAPS_RECEIVE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application>
<activity>
...
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxxxxxxxxxxxxxxxxxxxxxxxxx"/>
<uses-library android:name="com.google.android.maps"/>
</application>
</manifest>
Map xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/BackGroundColor"
android:orientation="vertical"
tools:context=".test" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingRight="10dp"
android:paddingTop="5dp" >
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="xxxxxxxxxxxxxxxxxxxxxxxxxx" >
</com.google.android.maps.MapView>
</LinearLayout>
</LinearLayout>
Map Activity:
public class Map extends MapActivity {
MapView mapView;
MapController mc;
GeoPoint p;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_map);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(true);
mc = mapView.getController();
String coordinates[] = {"1.352566007", "103.78921587"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
mapView.invalidate();
}
#Override
protected boolean isRouteDisplayed() { return false; }
}
I know that there are plenty of post with the same problem. I have read them all!!!
I have created the key several times but still the same...
Any ideas what is wrong and I am only seeing the grid? the application has the same outcome to AVD as well as to a Samsung mobile with android version 2.2.1
If you writing you application for API V2 then you are using the wrong objects, few things you need to fix:
1. Remove this:
<uses-library android:name="com.google.android.maps"/>
it's a permission for Google Map API V1.
2. you have to add this 2 permissions:
<permission android:name="your.application.package.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<uses-permission android:name="your.application.package.permission.MAPS_RECEIVE"/>
change: your.application.package to your current application package.
3. As you targeting SDK v8, you have to add fragment support to your application in order to use the SupportMapFragment from Google Map API V2, So you need to add google-support-v4 and change your map object to SupportMapFragment.
4. Your MapActivity has to be changed to FragmentActivity.
5. You can can get more information on how to make those changes in a blog post I wrote on how to add Google Map API V2 to your project:
Google Maps API V2
Your problem is related with the emulator, you need to install to APK's.
I had similar problems so I prepared step by step tutorial to show how you can use google map android v2 on emulator(android 4.2.2) have a look at to my blog: http://umut.tekguc.info/en/content/google-android-map-v2-step-step
You need to generate MD5 fingerprint from java keytool instead of SHA1.
Check this post to generate MD5 fingerprint:
How can I get the MD5 fingerprint from Java's keytool, not only SHA-1?

Getting started with osmdroid, mapview shows only grid

I am planning to use OSM in my android app and i decided to give osmdroid a try. Here is what i have done till now.
1: I downloaded the slf4j-android-1.5.8.jar and osmdroid-android-3.0.8.jar and copied them to my libs folder and added them to build path.
2: My Xml layout file code looks like this.
<org.osmdroid.views.MapView
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
3: I have added all the permissions to the manifest
<uses-permission android:name="android.permission.INTERNET"/>
<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" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
4: My on create method looks like this
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mapify);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setTileSource(TileSourceFactory.MAPNIK);
mapView.setBuiltInZoomControls(true);
mapView.setClickable(true);
mapController = mapView.getController();
mapController.setZoom(15);
}
My problem is that despite the code running fine i am not able to see the map when i run on my emulator. I am connected to internet and am able to see maps while using google map view.
I have look around the internet and also some questions in stackoverflow regarding same issue. Most of them either seem to have forgotten to add permissions or to add slf4j to build path. I have done both so i am at loss at what have i done wrong. Would be very grateful if you provided some insight.
Solved:
I don't know what is the reason but the map displays after i relaunch it a few times. After that everything seems to work fine. This happens again when i try in new virtual device. When i close and relaunch the app few times it starts working fine.
This is an old question, but in case somebody needs the answer. This may be caused if the emulator does not have an external storage to cache map tiles. I created an emulator with SD card and that solved the problem
open street maps (osmdroid) shows grey tiles not map in PC

osmdroid displays an empty grid

I wanted to try out having OpenStreetMap integration by osmdroid, but I got stuck at an issue where I have no idea what is missing. The scenario is as follows:
Symptom: Map widget gets displayed but only with an empty grid.
This is in my Activity class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MapView mapView = new MapView(this, 256);
mapView.setTileSource(TileSourceFactory.MAPNIK);
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);
setContentView(mapView);
}
(I tried selecting various TileSources with seemingly no difference)
My Manifest also includes these permissions (as a child of <manifest>, after <uses-sdk>):
<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" />
<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" />
...and my emulator is otherwise connected to the internet, eg. web browsers work.
osmdroid and slf4j are in the build path. The app compiles without a problem and generates no exceptions.
What could be wrong?
Many thanks!
SOLVED: This was caused by my emulator not having an external storage to cache map tiles.
I'm pretty new to OSMdroid too but try doing this?
setContentView(R.layout.offline_map_activity);
mapView = (MapView) findViewById(R.id.openmapview);
You would also need to have offline_map_activity.xml in your res/layouts:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<org.osmdroid.views.MapView
android:id="#+id/openmapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
Hope this works for you. At least this worked for my codes :)
i tink your Device has don't SD. You have change the Cache Path
OpenStreetMapTileProviderConstants.setCachePath(this.getFilesDir().getAbsolutePath());

Categories

Resources