如何在Android中的地图上显示多个叠加层?

发布于 2024-11-16 06:24:52 字数 121 浏览 2 评论 0原文

我有包含纬度经度的项目列表。我想在Google地图上显示项目列表。 那么,如何创建叠加列表以及如何在 Google 地图上显示叠加?

I have item list with latitude and longitude .I want to show the item list on Google map..
so, how to create overlay list and how to show overlays on Google map ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舂唻埖巳落 2024-11-23 06:24:52

这就是我在项目中所做的...

package org.nip.gmap;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

public class Main extends MapActivity {


    /** Called when the activity is first created. */

    MapView map;
    MapController controller;
    List<Overlay> overlayList;
    int lat=0;
    int lng=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        map = (MapView) findViewById(R.id.mapView);
        map.setBuiltInZoomControls(true);

        overlayList = map.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.pushpin2);
        CustomPinpoint itemizedoverlay = new CustomPinpoint(drawable,this);


        double lat_coordinates[] ={27.700556,28.2642635,30.0018168,29.776669,29.4096819,29.4560611};
        double lng_coordinates[] ={85.3630,83.9735195,80.7382742,81.2518833,81.8115051,80.5403779};
        String place_name[] ={"kathmandu","Pokhara","Darchula","Bajhang","Bajura","Baitadi"};
        String place_info[] ={"Its an capital of Nepal","Its and tourist place of Nepal","Its one of the beautiful place in country side","CHD District Target:10 51,960, VDCs/Muncipalities reported:41/41","CHD District Target: 71,280, VDCs/Muncipalities reported: 47/47","CHD District Target:10 51,960, VDCs/Muncipalities reported:41/41","CHD District Target: 71,280, VDCs/Muncipalities reported: 7/7","CHD District Target:10 21,960, VDCs/Muncipalities reported:44/41","CHD District Target: 33,3123, VDCs/Muncipalities reported: 47/47"};

         try{
            for(int i=0; i<place_name.length; i++)
             {
                 GeoPoint point = new GeoPoint((int)(lat_coordinates[i]*1E6),(int)(lng_coordinates[i]*1E6));
                 OverlayItem overlayitem = new OverlayItem(point, place_name[i], place_info[i]);
                 itemizedoverlay.addOverlay(overlayitem);
             }
         }catch(NullPointerException e){
             e.getStackTrace();

         }
         finally{
            overlayList.add(itemizedoverlay);
         }

        controller = map.getController();
        controller.animateTo(new GeoPoint((int)(lat_coordinates[0]*1E6),(int)(lng_coordinates[0]*1E6)));
        controller.setZoom(8);


    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

}

创建新类...

package org.nip.gmap;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

//import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class CustomPinpoint extends ItemizedOverlay<OverlayItem> {

    private ArrayList<OverlayItem> pinpoints = new ArrayList<OverlayItem>();
    private Context c;


    public CustomPinpoint(Drawable defaultMarker, Context context) {
        super(boundCenter(defaultMarker));
        c= context;

    }

//  public CustomPinpoint(Drawable M, Context context) {
//      
//      this(M);
//      c= context;
//  }

    public void addOverlay (OverlayItem overlay)
    {
        pinpoints.add(overlay);

        populate();
    }

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

    @Override
    public int size() {

        // TODO Auto-generated method stub
        return pinpoints.size();
    }
    @Override
    protected boolean onTap(int index) {
        // TODO Auto-generated method stub
        OverlayItem item = pinpoints.get(index);
        AlertDialog.Builder dialog = new AlertDialog.Builder(c);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.show();
        return true;

    }


}

图钉的 OnClickListener

This is what i did on my project...

package org.nip.gmap;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

public class Main extends MapActivity {


    /** Called when the activity is first created. */

    MapView map;
    MapController controller;
    List<Overlay> overlayList;
    int lat=0;
    int lng=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        map = (MapView) findViewById(R.id.mapView);
        map.setBuiltInZoomControls(true);

        overlayList = map.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.pushpin2);
        CustomPinpoint itemizedoverlay = new CustomPinpoint(drawable,this);


        double lat_coordinates[] ={27.700556,28.2642635,30.0018168,29.776669,29.4096819,29.4560611};
        double lng_coordinates[] ={85.3630,83.9735195,80.7382742,81.2518833,81.8115051,80.5403779};
        String place_name[] ={"kathmandu","Pokhara","Darchula","Bajhang","Bajura","Baitadi"};
        String place_info[] ={"Its an capital of Nepal","Its and tourist place of Nepal","Its one of the beautiful place in country side","CHD District Target:10 51,960, VDCs/Muncipalities reported:41/41","CHD District Target: 71,280, VDCs/Muncipalities reported: 47/47","CHD District Target:10 51,960, VDCs/Muncipalities reported:41/41","CHD District Target: 71,280, VDCs/Muncipalities reported: 7/7","CHD District Target:10 21,960, VDCs/Muncipalities reported:44/41","CHD District Target: 33,3123, VDCs/Muncipalities reported: 47/47"};

         try{
            for(int i=0; i<place_name.length; i++)
             {
                 GeoPoint point = new GeoPoint((int)(lat_coordinates[i]*1E6),(int)(lng_coordinates[i]*1E6));
                 OverlayItem overlayitem = new OverlayItem(point, place_name[i], place_info[i]);
                 itemizedoverlay.addOverlay(overlayitem);
             }
         }catch(NullPointerException e){
             e.getStackTrace();

         }
         finally{
            overlayList.add(itemizedoverlay);
         }

        controller = map.getController();
        controller.animateTo(new GeoPoint((int)(lat_coordinates[0]*1E6),(int)(lng_coordinates[0]*1E6)));
        controller.setZoom(8);


    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

}

Create new class...

package org.nip.gmap;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

//import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class CustomPinpoint extends ItemizedOverlay<OverlayItem> {

    private ArrayList<OverlayItem> pinpoints = new ArrayList<OverlayItem>();
    private Context c;


    public CustomPinpoint(Drawable defaultMarker, Context context) {
        super(boundCenter(defaultMarker));
        c= context;

    }

//  public CustomPinpoint(Drawable M, Context context) {
//      
//      this(M);
//      c= context;
//  }

    public void addOverlay (OverlayItem overlay)
    {
        pinpoints.add(overlay);

        populate();
    }

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

    @Override
    public int size() {

        // TODO Auto-generated method stub
        return pinpoints.size();
    }
    @Override
    protected boolean onTap(int index) {
        // TODO Auto-generated method stub
        OverlayItem item = pinpoints.get(index);
        AlertDialog.Builder dialog = new AlertDialog.Builder(c);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.show();
        return true;

    }


}

OnClickListener for pushpin

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文