我的目的是创建一个充当指南的应用程序。我的第一步是获取地图,添加一些重要的名胜古迹,并使用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.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;
}
}
以上两个代码仅显示没有标记的带有卫星视图的地图。 有人可以帮忙......?
答案 0 :(得分:0)
使用以下链接的代码,它可能对您有帮助。
答案 1 :(得分:0)
我在我的应用程序中做了类似的事情,对我来说效果很好。