ItemizedOverlays Android地图不起作用

时间:2012-04-24 01:28:37

标签: android itemizedoverlay

我正在尝试在mapview上添加多个标记,但我无法使其工作,它没有 当我评论调用LoadMarkers()的方法时,似乎甚至加载了mapview。 你能告诉我我的代码有什么问题吗?

public class MyATMLocatorActivity extends MapActivity {
    /** Called when the activity is first created. */

    private MapView mapView;
    private MapController myMapController;
    private GeoPoint myGeoPoint;
    private LocationManager myLocationManager;
    private LocationListener myLocationListener;

    public static Context context;
    private MyItemizedOverlay myItemizedOverlay = null;
    private MyLocationOverlay myLocationOverlay = null;

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

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

        myMapController = mapView.getController();

        this.LoadMarkers();

    }

这是加载标记的方法..

private void LoadMArkers() {

        mapView.getOverlays().clear();
        mapView.postInvalidate();

        OverlayItem overlayItem = null;

        Drawable myATMPic = getResources().getDrawable(R.drawable.atmicon);
        myATMPic.setBounds(0, 0, myATMPic.getIntrinsicWidth(), myATMPic.getIntrinsicHeight());

        // Super España
        myItemizedOverlay = new MyItemizedOverlay(myATMPic);
        myGeoPoint = new GeoPoint((int) (-25.353043), (int) (-57.444495));
        overlayItem = new OverlayItem(myGeoPoint, "Supermercado Espana","Capiatá");
        myItemizedOverlay.addOverlay(overlayItem);
        mapView.getOverlays().add(myItemizedOverlay);

        // Martín Ledesma
        myItemizedOverlay = new MyItemizedOverlay(myATMPic);
        myGeoPoint = new GeoPoint((int) (-25.353974), (int) (-57.445214));
        overlayItem = new OverlayItem(myGeoPoint, "Martín Ledesma", "Capiatá");
        myItemizedOverlay.addOverlay(overlayItem);
        mapView.getOverlays().add(myItemizedOverlay);               

        mapView.postInvalidate();
    }

这是我的ItemizedOverlay()类

public class MyItemizedOverlay extends com.google.android.maps.ItemizedOverlay<OverlayItem> {

    private Context mContext;   
    private ArrayList<OverlayItem> myOverlaysArray = new ArrayList<OverlayItem>();;

    private GeoPoint geoPoint = null;

    public MyItemizedOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));        
    }

     public MyItemizedOverlay(Drawable defaultMarker, Context context) {
         super(boundCenterBottom(defaultMarker));
         mContext = context;
   }

    public void addOverlay(OverlayItem overlay) {
        myOverlaysArray.add(overlay);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return myOverlaysArray.get(i);
    }

    // Removes overlay item i
    public void removeItem(int i) {
        myOverlaysArray.remove(i);
        populate();
    }

    // Returns present number of items in list
    @Override
    public int size() {
        return myOverlaysArray.size();
    }

    public void addOverlayItem(OverlayItem overlayItem) {
        myOverlaysArray.add(overlayItem);
        populate();
    }

    public void addOverlayItem(int lat, int lon, String title) {
        try {
            geoPoint = new GeoPoint(lat, lon);
            OverlayItem overlayItem = new OverlayItem(geoPoint, title, null);
            addOverlayItem(overlayItem);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected boolean onTap(int index) {
        OverlayItem item = myOverlaysArray.get(index);
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.show();
        return true;
    }

}

1 个答案:

答案 0 :(得分:0)

您是否在android manifest.xml中添加了正确的代码? 使用MapView将com.google.android.maps作为子项添加到application元素。

<uses-library android:name="com.google.android.maps" />

将INTERNET作为子元素添加到元素中。

<uses-permission android:name="android.permission.INTERNET" />

您无需创建2个逐项列表叠加层。此外,您需要将所有GPS坐标乘以1e6,然后再将它们转换为整数。在您之前的代码中,两个GPS坐标将被转换为相同的整数对值,并且标记将被放置在地图上的相同位置(-25,-57),并且只有最后一个将是可触摸的。另外,不要忘记将应用程序的上下文传递给明细化的叠加层。

MyATMLocatorActivity.java

package maps.test;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

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.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MyATMLocatorActivity extends MapActivity {
    /** Called when the activity is first created. */

    private MapView mapView;
    private MapController myMapController;
    private GeoPoint myGeoPoint;
    private GeoPoint myGeoPoint2;
    private LocationManager myLocationManager;
    private LocationListener myLocationListener;

    public static Context context;
    private MyItemizedOverlay myItemizedOverlay = null;
    private final MyLocationOverlay myLocationOverlay = null;

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

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

        myMapController = mapView.getController();

        this.LoadMarkers();

    }

    private void LoadMarkers() {

        List<Overlay> overlays = mapView.getOverlays();
        mapView.postInvalidate();

        OverlayItem overlayItem = null;

        Drawable myATMPic = getResources().getDrawable(R.drawable.ic_launcher);
        myATMPic.setBounds(0, 0, myATMPic.getIntrinsicWidth(), myATMPic.getIntrinsicHeight());
        myItemizedOverlay = new MyItemizedOverlay(myATMPic, this);

        // Super España
        myGeoPoint = new GeoPoint((int) (-25.353043*1e6), (int) (-57.444495*1e6));
        overlayItem = new OverlayItem(myGeoPoint, "Supermercado Espana","Capiatá");
        myItemizedOverlay.addOverlay(overlayItem);

        // Martín Ledesma
        myGeoPoint = new GeoPoint((int) (25.353974*1e6), (int) (-57.445214*1e6));
        overlayItem = new OverlayItem(myGeoPoint2, "Martín Ledesma", "Capiatá");
        myItemizedOverlay.addOverlay(overlayItem);

        overlays.add(myItemizedOverlay);
        mapView.postInvalidate();
    }


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

MyItemizedOverlay.java

package maps.test;

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.OverlayItem;

public class MyItemizedOverlay extends com.google.android.maps.ItemizedOverlay<OverlayItem> {

    private Context mContext;
    private final ArrayList<OverlayItem> myOverlaysArray = new ArrayList<OverlayItem>();
    private GeoPoint geoPoint = null;
    public MyItemizedOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
    }

    public MyItemizedOverlay(Drawable defaultMarker, Context context) {
        super(boundCenterBottom(defaultMarker));
        mContext = context;
    }

    public void addOverlay(OverlayItem overlay) {
        myOverlaysArray.add(overlay);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return myOverlaysArray.get(i);
    }

    // Removes overlay item i
    public void removeItem(int i) {
        myOverlaysArray.remove(i);
        populate();
    }

    // Returns present number of items in list
    @Override
    public int size() {
        return myOverlaysArray.size();
    }

    public void addOverlayItem(OverlayItem overlayItem) {
        myOverlaysArray.add(overlayItem);
        populate();
    }

    public void addOverlayItem(int lat, int lon, String title) {
        try {
            geoPoint = new GeoPoint(lat, lon);
            OverlayItem overlayItem = new OverlayItem(geoPoint, title, null);
            addOverlayItem(overlayItem);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected boolean onTap(int index) {
        OverlayItem item = myOverlaysArray.get(index);
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.show();
        return true;
    }
}

我已经在示例应用程序中测试了代码,这是我的Android清单,将其与您自己的清单进行比较。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="maps.test"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="com.google.android.maps" />
        <activity
            android:name=".MyATMLocatorActivity"
            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>

这是我的Main.xml

<?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:clickable="true"
    android:apiKey="Your Maps API Key goes here"
/>