0

我的意图是创建一个充当指南的应用程序。我的第一步是获取地图,添加一些重要的名胜古迹并使用 LBS 和邻近警报,通知感兴趣的地点并叙述与该地点相关的内容。我的问题是,标记没有显示在地图上。我遵循了 Pro Android 3 书中给出的内容。代码如下。

package com.project1;

import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;

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;

public class Testmap24Activity extends MapActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.d("Check point 1","Check Point 1");
        MapView mv = (MapView)findViewById(R.id.mapview);
        mv.setBuiltInZoomControls(true);
        mv.displayZoomControls(true);
        MapController mvc = mv.getController();
        //mv.setSatellite(true);
        mv.setTraffic(true);
        mvc.setZoom(15);

       Drawable drawable = getResources().getDrawable(R.drawable.marker1);
       drawable.setBounds(drawable.getIntrinsicWidth()/2, drawable.getIntrinsicHeight(), drawable.getIntrinsicWidth()/2, 0 );

       map24overlay m24 = new map24overlay(drawable);
       mv.getOverlays().add(m24);
       GeoPoint pt = m24.getCenterPt();
       int latSpan = m24.getLatSpanE6();
       int lonSpan = m24.getLonSpanE6();
       Log.v("Overlays", "Lat span is " + latSpan);
       Log.v("Overlays", "Lon span is " + lonSpan);
       mv.animate();

           }

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

覆盖的代码如下。

package com.project1;

import java.util.ArrayList;
import java.util.Iterator;

import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.Log;

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

public class map24overlay extends ItemizedOverlay {
    private ArrayList<OverlayItem> locations = new ArrayList<OverlayItem>();
    private GeoPoint center = null;

    public map24overlay(Drawable drawable) {
        // TODO Auto-generated constructor stub
        super(drawable);            
        GeoPoint grtgr1 = new GeoPoint((int)13.044101 * 1000000, (int)80.232723 * 100000);
        GeoPoint grtpp = new GeoPoint((int)13.044101 * 1000000, (int)80.232723 * 100000);
        GeoPoint grtgr2 = new GeoPoint((int)13.044101 * 1000000, (int)80.232723 * 100000);
        GeoPoint grtpt = new GeoPoint((int)13.044101 * 1000000, (int)80.232723 * 100000);
        locations.add(new OverlayItem(grtgr1, "GRT Coats Road", "GRT Coats Road"));
        locations.add(new OverlayItem(grtpp, "Panagal Park", "Panagal Park"));
        locations.add(new OverlayItem(grtgr2, "GRT Usman Road", "GRT Usman Road"));
        locations.add(new OverlayItem(grtpt, "Pothys", "Pothys"));
        populate();
    }

    public GeoPoint getCenterPt() {
        if(center == null) {
        int northEdge = -90000000; // i.e., -90E6 microdegrees
        int southEdge = 90000000;
        int eastEdge = -180000000;
        int westEdge = 180000000;
        Iterator<OverlayItem> iter = locations.iterator();
        while(iter.hasNext()) {
        GeoPoint pt = iter.next().getPoint();
        if(pt.getLatitudeE6() > northEdge)
        northEdge = pt.getLatitudeE6();
        Log.d("North Edge", " " + northEdge + " ");
        if(pt.getLatitudeE6() < southEdge)
        southEdge = pt.getLatitudeE6();
        Log.d("South Edge", " " + southEdge + " ");
        if(pt.getLongitudeE6() > eastEdge)
        eastEdge = pt.getLongitudeE6();
        Log.d("East Edge", " " + eastEdge + " ");
        if(pt.getLongitudeE6() < westEdge)
        westEdge = pt.getLongitudeE6();
        Log.d("West Edge", " " + westEdge + " ");
        }
        center = new GeoPoint((int)((northEdge +southEdge)/2),
        (int)((westEdge + eastEdge)/2));
        }
        return center;
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow)
    {
    // Hide the shadow by setting shadow to false
    super.draw(canvas, mapView, shadow);
    }

    @Override
    protected OverlayItem createItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return 0;
    }

}

上面的两个代码只显示没有标记的卫星视图地图。有人可以帮忙吗...?

4

2 回答 2

0

我在我的应用程序中做了类似的事情,它对我来说效果很好。

你可以在这里看到样本

于 2013-07-11T15:47:42.880 回答
0

为此,请使用以下链接的代码,它可能会对您有所帮助。

地图叠加演示

于 2012-06-12T05:00:37.010 回答