我编写了教程以在谷歌地图中搜索地址。不幸的是,当我缩小时,标记并没有粘在那个地方。标记似乎正在飘走。
class MapOverlay extends com.google.android.maps.Overlay {
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 red marker
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker_red);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-32, null);
return true;
}
}
我认为它来自将 GeoPoint 转换为屏幕像素这是来自搜索按钮的 onClick 事件的代码:
this.searchButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
followMe = false;
String temp = searchBar.getText().toString();
List<Address> addresses = null;
try {
addresses = geoCoder.getFromLocationName(temp,5);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (addresses != null) {
p = new GeoPoint(
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
mapController.animateTo(p);
mapController.setZoom(19);
MapOverlay mapOverlay = new MapOverlay();
mapOverlays = mapView.getOverlays();
mapOverlays.add(mapOverlay);
mapView.postInvalidate();
//searchBar.setText("");
} else {
AlertDialog.Builder adb = new AlertDialog.Builder(
GMapsSubActivity.this);
adb.setTitle("Google Map");
adb.setMessage("Please Provide the Proper Place");
adb.setPositiveButton("Close", null);
adb.show();
}
}
});
searchbar 是一个 editText 和 followMe 不相关。我希望标记贴在我缩小时寻找的位置。
图片: http : //i.imgur.com/iavqa.jpg http://i.imgur.com/dFqIs.jpg