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());
Related
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.
I'm trying to implement google maps V2 but the maps is showing blank. I've edited the manifest, created xml layout and the activity but maps is showing blank.
Here is what i did to the manifest:
<permission
android:name="com.biznismap.com.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.biznismap.com.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
and added this in as the first child of application node in the manifest:
<uses-library android:name="com.google.android.maps" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="my_key" />
Here is my map xml layout:
<?xml version="1.0" encoding="utf-8"?>
<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:apiKey="my_key"
android:clickable="true" />
Here is my MapActivity:
public class ListingMap extends MapActivity {
private HelloItemizedOverlay itemizedoverlay;
private List<Overlay> mapOverlays;
private String lat, lng;
#Override
protected boolean isRouteDisplayed() {
return false;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listing_map);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(
R.drawable.ic_launcher);
itemizedoverlay = new HelloItemizedOverlay(drawable);
getExtras();
}
private void getExtras() {
Bundle extras = getIntent().getExtras();
lat = extras.getString("lat");
lng = extras.getString("lng");
Log.v("--", lat+" "+lng);
float latitude=Float.valueOf(lat);
float longitude=Float.valueOf(lng);
GeoPoint point = new GeoPoint((int)(latitude * 1E6),(int)(longitude * 1E6));
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!",
"I'm in Mexico City!");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
}
So everything is going OK but, when I want to display something, the map is blank, and I get this from the emulator:
06-02 17:09:26.921: E/MapActivity(1772): Couldn't get connection factory client
So where am I going wrong, and how can I fix this problem?
You have two problems here. In v2 you may not use
<uses-library android:name="com.google.android.maps" />
, and your layout is invalid. If you want full-layout map, just use this:
<FrameLayout
... >
<fragment
...
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</FrameLayout>
On your Java code, use FragmentActivity and:
map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.your_id)).getMap();
This way, you will get compatibity with pre-honeycomb devices. Good luck!
Here is what https://developers.google.com/maps/documentation/android/v1/hello-mapview says:
Note: Version 1 of the Google Maps Android API has been officially deprecated as of December 3rd, 2012. This means that from March 18th, 2013 you will no longer be able to request an API key for this version. No new features will be added to Google Maps Android API v1. However, apps using v1 will continue to work on devices. Existing and new developers are encouraged to use Google Maps Android API v2.
Your code is miximg v1 and v2 properties.
Either you don't have internet connectivity (my guess), or the key you entered in incorrect. Double check both these things.
Also, ensure the emulator you are using to test (if applicable) has the Google APIs installed.
MapView is no more used with API v2. Instead use :
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
Accordingly, use FragmentActivity instead of MapActivity and inflate the map.
You can check Loading Map using Google Map API V2 for more details
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?
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
I am trying to add addwhirl in my app but I doesnt show up any ads, I am doing following things
Manifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Layout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/com.liverpool.activities"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/layout_ad" />
in code I am doing following
try
{
AdManager.setTestDevices(new String[] { AdManager.TEST_EMULATOR });
LinearLayout layout = (LinearLayout) findViewById(R.id.layout_ad);
AdWhirlLayout adWhirlLayout = new AdWhirlLayout(this, "SDK key from adwhirl");
Display d = this.getWindowManager().getDefaultDisplay();
RelativeLayout.LayoutParams adWhirlLayoutParams = new RelativeLayout.LayoutParams(d.getWidth(), 72);
layout.addView(adWhirlLayout, adWhirlLayoutParams);
}
catch (Exception e)
{
}
where I am doing wrong?
Remember you only really need the INTERNET permission. The rest is optional. You shouldn't ask for GPS location unless your app needs it. If you do it just for the ads you'll get negative reactions from your users. And trust me adding more geo targeting will not increase your revenue.
There is nothing wrong with the above code.
Problem is that I haven't configured the adwhirl page with ads provider settings its very fool to miss such thing