我无法在使用Eclipse的google map-Android应用程序上显示标记(pushpin.gif)
我将一个pushpin.gif复制到适当的文件夹:project/res/drawable mdpi/pushpin.gif 我无法在地图上显示标记,以下是我使用的代码:我无法在使用Eclipse的google map-Android应用程序上显示标记(pushpin.gif),android,eclipse,google-maps-markers,Android,Eclipse,Google Maps Markers,我将一个pushpin.gif复制到适当的文件夹:project/res/drawable mdpi/pushpin.gif 我无法在地图上显示标记,以下是我使用的代码: import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MenuItem; import android.support.v4.app.NavUtils; import com.go
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.GeoPoint;
import android.view.KeyEvent;
import com.google.android.maps.MapController;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import com.google.android.maps.Overlay;
import java.util.List;
import android.view.MotionEvent;
import android.widget.Toast;
import android.location.Address;
import android.location.Geocoder;
import java.util.Locale;
import java.io.IOException;
public class MainActivity extends MapActivity {
MapView mapView;
MapController mc;
GeoPoint p;
class MapOverlay extends com.google.android.maps.Overlay
{
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pushpin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
/* --To display the lat & long on the screen--
Toast.makeText(getBaseContext(), "Location: "+ p.getLatitudeE6() / 1E6 + "," + p.getLongitudeE6() /1E6 ,Toast.LENGTH_SHORT).show();*/
/*Geo Locating the empire state
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocationName("empire state building", 5);
String add = "";
if (addresses.size() > 0) {
p = new GeoPoint(
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
mc.animateTo(p);
mapView.invalidate();
}
} catch (IOException e) {
e.printStackTrace();
}*/
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
p.getLatitudeE6() / 1E6,
p.getLongitudeE6() / 1E6, 1);
String add = "";
if (addresses.size() > 0)
{
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
add += addresses.get(0).getAddressLine(i) + "\n";
}
Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
e.printStackTrace();
}
return true;
}
return false;
}
}
//添加位置标记
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
//Method to force the MapView to be redrawn
mapView.invalidate();
//To display traffic conditions on the Map
// mapView.setTraffic(true);
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
MapController mc = mapView.getController();
switch (keyCode)
{
case KeyEvent.KEYCODE_3:
mc.zoomIn();
break;
case KeyEvent.KEYCODE_1:
mc.zoomOut();
break;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
MapOverlay MapOverlay=新建MapOverlay();
List ListoForLays=mapView.getOverlays();
listOfOverlays.clear();
添加(映射覆盖);
//方法强制重新绘制地图视图
mapView.invalidate();
//在地图上显示交通状况的步骤
//mapView.setTraffic(true);
}
公共布尔onKeyDown(int-keyCode,KeyEvent事件)
{
MapController mc=mapView.getController();
开关(钥匙代码)
{
案例KeyEvent.KEYCODE_3:
mc.zoomIn();
打破
案例KeyEvent.KEYCODE_1:
mc.zoomOut();
打破
}
返回super.onKeyDown(keyCode,event);
}
@凌驾
受保护的布尔值isRouteDisplayed(){
//TODO自动生成的方法存根
返回false;
}
}我找到了一个示例项目,解释了我面临的所有问题
我添加了类MyMapOverlays扩展com.google.android.maps.Overlay{GeoPoint location=null;公共MyMapOverlays(GeoPoint location){super();this.location=location;}
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
//Method to force the MapView to be redrawn
mapView.invalidate();
//To display traffic conditions on the Map
// mapView.setTraffic(true);
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
MapController mc = mapView.getController();
switch (keyCode)
{
case KeyEvent.KEYCODE_3:
mc.zoomIn();
break;
case KeyEvent.KEYCODE_1:
mc.zoomOut();
break;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
class MyMapOverlays extends com.google.android.maps.Overlay
{
GeoPoint location = null;
public MyMapOverlays(GeoPoint location)
{
super();
this.location = location;
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
super.draw(canvas, mapView, shadow);
//translate the screen pixels
Point screenPoint = new Point();
mapView.getProjection().toPixels(this.location, screenPoint);
//add the image
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.our_cross_image),
screenPoint.x, screenPoint.y , null); //Setting the image location on the screen (x,y).
}
}