【问题标题】:How can add overlay to map on android?如何在android上添加叠加层到地图?
【发布时间】:2012-12-21 10:51:33
【问题描述】:

我尝试应用此link 中的这些代码。但是我遇到了 NullPointerException 之类的错误。

我使用碎片,这是我的 BaseFragment 中的函数

 public void createMapViewForHaritaFragment(IlanList list)
{
    IlanList gelenListe;
    gelenListe=list;

    if(gelenListe!=null)
    {
        MainActivity parent = getParentActivity();
        mMapViewContainer = parent.getmMapViewContainer();
        mMapView = parent.getmMapView();

        /**/
            mc = mMapView.getController();

            LocationManager lm = (LocationManager) parent.getSystemService(parent.getApplicationContext().LOCATION_SERVICE); 
            Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            String longitude = String.valueOf(location.getLongitude()).substring(0, 9);
            String latitude = String.valueOf(location.getLatitude()).substring(0, 9);


            String coordinates[] = {latitude,longitude};
            double lat = Double.parseDouble(coordinates[0]);
            double lng = Double.parseDouble(coordinates[1]);

            p = new GeoPoint(
                (int) (lat * 1E6), 
                (int) (lng * 1E6));

            mc.animateTo(p);
            mMapView.setBuiltInZoomControls(true);      

            mMapView.setSatellite(false);               
            mc.setZoom(16);         




            /*Harita
            public View mMapViewContainer;
            public MapView mMapView;
            public MapController mc;
            public GeoPoint p;

            public List<Overlay> mapOverlays;
            public Drawable drawable;
            public MapItemizedOverlay itemizedOverlay;  
            Harita*/

            drawable = this.getResources().getDrawable(R.drawable.map_kirmizi_png);
            itemizedOverlay = new MapItemizedOverlay(drawable, parent.getApplicationContext(), 30);//text size: 30

            GeoPoint gPointMe = new GeoPoint(41099932,29002657);
            OverlayItem overlayItem = new OverlayItem(gPointMe, "Me", "This is my location");
            itemizedOverlay.addOverlay(overlayItem);    

            mapOverlays.add(itemizedOverlay);

            mMapView.setBuiltInZoomControls(true);

            //move map over to my position
            mc.animateTo(gPointMe);  



        mMapView.invalidate();
    }

}

我想,我在处理这段代码时遇到了错误:

protected OverlayItem createItem(int i)
{
    return mOverlays.get(i);
}

这些是我的 LogCat。

12-21 12:35:53.443: E/AndroidRuntime(19782): FATAL EXCEPTION: main
12-21 12:35:53.443: E/AndroidRuntime(19782): java.lang.NullPointerException
12-21 12:35:53.443: E/AndroidRuntime(19782):    at com.mert.fragment.BaseFragment.createMapViewForHaritaFragment(BaseFragment.java:274)
12-21 12:35:53.443: E/AndroidRuntime(19782):    at com.mert.fragment.impl.EtrafimdaHaritaFragment$MyHandler.handleMessage(EtrafimdaHaritaFragment.java:154)
12-21 12:35:53.443: E/AndroidRuntime(19782):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 12:35:53.443: E/AndroidRuntime(19782):    at android.os.Looper.loop(Looper.java:143)
12-21 12:35:53.443: E/AndroidRuntime(19782):    at android.app.ActivityThread.main(ActivityThread.java:4277)
12-21 12:35:53.443: E/AndroidRuntime(19782):    at java.lang.reflect.Method.invokeNative(Native Method)
12-21 12:35:53.443: E/AndroidRuntime(19782):    at java.lang.reflect.Method.invoke(Method.java:507)
12-21 12:35:53.443: E/AndroidRuntime(19782):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-21 12:35:53.443: E/AndroidRuntime(19782):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-21 12:35:53.443: E/AndroidRuntime(19782):    at dalvik.system.NativeStart.main(Native Method)

感谢您的帮助。

【问题讨论】:

    标签: android google-maps overlay


    【解决方案1】:
            AddItemized Overlay class:
    
            public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {
    
                private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
    
                private Context context;
    
                public AddItemizedOverlay(Drawable defaultMarker) {
                     super(boundCenterBottom(defaultMarker));
                }
    
                public AddItemizedOverlay(Drawable defaultMarker, Context context) {
                     this(defaultMarker);
                     this.context = context;
                }
    
                @Override
                protected OverlayItem createItem(int i) {
                   return mapOverlays.get(i);
                }
    
                @Override
                public int size() {
                   return mapOverlays.size();
                }
    
                @Override
                protected boolean onTap(int index) {
                   Log.e("Tap", "Tap Performed");
                   return true;
                }
    
                public void addOverlay(OverlayItem overlay) {
                   mapOverlays.add(overlay);
                    this.populate();
                }
    
             }
    
    
        Use this code in your Activity
    
    
          List<Overlay> mapOverlays = mv.getOverlays();
                Drawable drawable = this.getResources().getDrawable(R.drawable.drawable);
                AddItemizedOverlay itemizedOverlay =
                new AddItemizedOverlay(drawable, this);
                OverlayItem overlayitem = new OverlayItem(gp, "Hello", "I am here");
                itemizedOverlay.addOverlay(overlayitem);
                mapOverlays.add(itemizedOverlay);
    
    
     public void createMapViewForHaritaFragment(IlanList list)
    {
        IlanList gelenListe;
        gelenListe=list;
    
        if(gelenListe!=null)
        {
            MainActivity parent = getParentActivity();
            mMapViewContainer = parent.getmMapViewContainer();
            mMapView = parent.getmMapView();
    
            /**/
                mc = mMapView.getController();
    
                LocationManager lm = (LocationManager) parent.getSystemService(parent.getApplicationContext().LOCATION_SERVICE); 
                Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                String longitude = String.valueOf(location.getLongitude()).substring(0, 9);
                String latitude = String.valueOf(location.getLatitude()).substring(0, 9);
    
    
                String coordinates[] = {latitude,longitude};
                double lat = Double.parseDouble(coordinates[0]);
                double lng = Double.parseDouble(coordinates[1]);
    
                p = new GeoPoint(
                    (int) (lat * 1E6), 
                    (int) (lng * 1E6));
    
            mc = mMapView.getController();
            mc.setCenter(p);
            mc.setZoom(16);
    
            List<Overlay> mapOverlays = mv.getOverlays();
            Drawable drawable = this.getResources().getDrawable(R.drawable.pin);
            AddItemizedOverlay itemizedOverlay = new AddItemizedOverlay(drawable,
                    this);
            OverlayItem overlayitem = new OverlayItem(p, "Hello", "I AM HERE");
            itemizedOverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedOverlay);
    
               /* mc.animateTo(p);
                mMapView.setBuiltInZoomControls(true);      
    
               mMapView.setSatellite(false);               
               mc.setZoom(16);   */      
    
    
    
    
                /*Harita
                public View mMapViewContainer;
                public MapView mMapView;
                public MapController mc;
                public GeoPoint p;
    
                public List<Overlay> mapOverlays;
                public Drawable drawable;
                public MapItemizedOverlay itemizedOverlay;  
                Harita*/
    
              /*  drawable = this.getResources().getDrawable(R.drawable.map_kirmizi_png);
                itemizedOverlay = new MapItemizedOverlay(drawable, parent.getApplicationContext(), 30);//text size: 30
    
                GeoPoint gPointMe = new GeoPoint(41099932,29002657);
                OverlayItem overlayItem = new OverlayItem(gPointMe, "Me", "This is my location");
                itemizedOverlay.addOverlay(overlayItem);    
    
                mapOverlays.add(itemizedOverlay);
    
                mMapView.setBuiltInZoomControls(true);
    
                //move map over to my position
                mc.animateTo(gPointMe);  
    
    
    
            mMapView.invalidate(); */
        }
    
    }
    

    【讨论】:

    • 感谢您的回答,好的,我会尝试但是,如何定义覆盖的位置?
    • 你获取纬度和经度的位置值
    • 是的,我得到了它们,但是如何在 Activity 中的这段代码中使用它们?
    • 我尝试了您的代码,并且成功添加了叠加层:) 非常感谢,我将尝试为来自 IlanList 类的每个位置添加叠加层。但是我有一个问题,我看不到“你好”或“我在这里”的文字?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    相关资源
    最近更新 更多