0

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

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;
    }

}
4

1 回答 1

0

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

<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"
/>
于 2012-04-24T01:44:26.840 回答