简体   繁体   English

如何在Android中使用osmdroid地图?

[英]How to use osmdroid maps in Android?

I am building application and I want to use osmdroid maps in it because I have heard that it doesn't require Google Play Services to use map. 我正在构建应用程序,并且想在其中使用osmdroid映射 ,因为听说它不需要Google Play服务即可使用映射。

What I have tried so far? 到目前为止我尝试过什么?

I downloaded .jar files, See the screenshots. 我下载了.jar文件,请参见屏幕截图。 But now don't know how to integrate it with my on going project. 但是现在不知道如何将其与正在进行的项目集成。

在此处输入图片说明在此处输入图片说明

I have searched on the internet but couldn't find the proper solution. 我已经在互联网上进行搜索,但是找不到合适的解决方案。 There was written to copy these files into libs folder. 编写将这些文件复制到libs文件夹中。 Is it a libs folder of my project which is ../AndroidStudioProjects/MyProject/app/libs ? 它是我项目的libs文件夹,它是../AndroidStudioProjects/MyProject/app/libs吗?

What should I do? 我该怎么办? I am using Android Studio 我正在使用Android Studio

  1. Put osmdroid-android-xx jar under lib folder of project directory 将osmdroid-android-xx jar放在项目目录的lib文件夹下
  2. In Android studio, it will reflect under lib folder now Right click it and hit 'Add as library' 3.Ensure that compile files('libs/smdroid-android-xxjar') is in your build.gradle file 在Android Studio中,它将立即反映在lib文件夹下。右键单击它,然后单击“添加为库”。3.确保build.gradle文件中包含编译文件('libs / smdroid-android-xxjar')。
  3. Do a clean build 做一个干净的构建

Now in MainActivty of your application/module: add the following code 现在在您的应用程序/模块的MainActivty中:添加以下代码

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_osmdroid);
        map = (MapView) findViewById(R.id.osm_map);
        map.setTileSource(TileSourceFactory.MAPQUESTOSM);
        map.setBuiltInZoomControls(true);
        map.setMultiTouchControls(true);
        map.setUseDataConnection(true);
        map.getTileProvider().clearTileCache();
        IMapController mapController = map.getController();
        mapController.setZoom(8);
        GeoPoint startPoint = new GeoPoint(24.084082, 79.892578);
        mapController.setCenter(startPoint);
    }

Meanwhile create activity_osmdroid.xml in layout folder and add the following code: 同时在布局文件夹中创建activity_osmdroid.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/osm_map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

finally, you must add permissions to your AndroidManifest.xml 最后,您必须向您的AndroidManifest.xml添加权限

<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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM