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

子要素としてインターネットを要素に追加します。

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

2つの項目別オーバーレイを作成する必要はありません。また、intにキャストする前に、すべてのGPS座標に1e6を掛ける必要があります。以前のコードでは、両方のGPS座標が同じ整数ペア値にキャストされ、マーカーはマップ上の同じ場所(-25、-57)に配置され、最後の1つだけがタッチ可能になります。また、アプリケーションのコンテキストを項目別オーバーレイに渡すことを忘れないでください。

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 に答える