OSMDroid地图没有显示 - 只有小灰色瓷砖

时间:2013-06-24 15:59:05

标签: android android-maps osmdroid openstreetmap

        package com.andersoncouncil.osm_01;

    import org.osmdroid.DefaultResourceProxyImpl;
    import org.osmdroid.ResourceProxy;
    import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
    import org.osmdroid.util.GeoPoint;
    import org.osmdroid.views.MapView;

    import android.app.Activity;
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.widget.TextView;

    public class MainActivity extends Activity {

        private TextView tv_sample;
        private MapView mapview_osm;
        private CustomItemizedOverlay item_overlay=null;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            tv_sample=(TextView)findViewById(R.id.textview_sample);
            mapview_osm=(MapView)findViewById(R.id.mapview);

            ResourceProxy proxy=new DefaultResourceProxyImpl(getApplicationContext());      

            Drawable icon=getResources().getDrawable(R.drawable.ic_launcher);

            int icon_height=icon.getIntrinsicHeight();
            int icon_width=icon.getIntrinsicWidth();
            icon.setBounds(0,icon_height,icon_width,0);
            item_overlay=new CustomItemizedOverlay(icon, proxy);
            mapview_osm.setTileSource(TileSourceFactory.MAPNIK);
            //mapview_osm.setUseDataConnection(true);
            mapview_osm.setBuiltInZoomControls(true);       
            mapview_osm.getOverlays().add(item_overlay);

            GeoPoint g1=new GeoPoint(5*1000000, 5*1000000);
            GeoPoint g2=new GeoPoint(50*100000, 70*1000000);
            item_overlay.addItem(g1,"point 1","point 1");
            item_overlay.addItem(g2, "point 2", "point 2"); 

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

    }


package com.andersoncouncil.osm_01;

import java.util.ArrayList;

import org.osmdroid.ResourceProxy;
import org.osmdroid.api.IMapView;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.overlay.ItemizedOverlay;
import org.osmdroid.views.overlay.OverlayItem;

import android.graphics.Point;
import android.graphics.drawable.Drawable;

public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem>{

    private ArrayList<OverlayItem> item_list=new ArrayList<OverlayItem>();

    public CustomItemizedOverlay(Drawable pDefaultMarker,
            ResourceProxy pResourceProxy) {
        super(pDefaultMarker, pResourceProxy);
        // TODO Auto-generated constructor stub
    }

    public void addItem(GeoPoint p, String tittle, String snippets){
        OverlayItem item=new OverlayItem(tittle, snippets, p);
        item_list.add(item);
        populate();
    }

    @Override
    public boolean onSnapToItem(int arg0, int arg1, Point arg2, IMapView arg3) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    protected OverlayItem createItem(int arg0) {
        // TODO Auto-generated method stub
        return item_list.get(arg0);
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return item_list.size();
    }

}

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textview_sample"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="OSM MAP EXAMPLE"
        android:layout_alignParentTop="true" />

    <org.osmdroid.views.MapView
        android:id="@+id/mapview"
        android:layout_below="@id/textview_sample"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true" />


</RelativeLayout>

我上面附上了我的代码。它只是OSMDroid的一个简单实现,在两个地方显示了启动器图标。但我的应用程序不播放任何地图。它只显示网格(地图网格,而不是实际位置)。

此程序清单文件添加如下:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.andersoncouncil.osm_01.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

在这里的任何人,谁知道问题或可以检测到它,请回复。

1 个答案:

答案 0 :(得分:0)

什么版本的osmdroid?你的目标api是什么?如果您使用API​​ 23及更高版本作为目标,则您已明确要求用户获得Internet访问和文件系统存储的许可。最新的osmdroid示例应用程序有一个例子,说明了所需的内容。 Android文档也有一个很好的例子。