gpt4 book ai didi

Android MapActivity 和 ItemizedOverlay 在 TouchEvent 上改变当前位置

转载 作者:太空狗 更新时间:2023-10-29 15:15:52 24 4
gpt4 key购买 nike

我有一个应用程序,其中一个 Activity 显示 map ,另一个类用于在 map 上放置标记或任何叠加项。它工作得很好,但现在当用户触摸 map 时,它会显示一 strip 有新位置的消息(此代码在 AddItemizedOverlay 的 ontouche 事件上实现)。

我想改为更改 map 上新位置的图钉(主要 Activity 中的代码)。如何将新位置数据从 AddItemizedOverlay 类传递到主要 Activity ?如何绘制新用户位置?谢谢!!!

myMapactivity.java

public class myMapactivity extends MapActivity 
{

private MapView mapView;
private MapController mapCtrl;
List<Overlay> mapOverlays;
GeoPoint mePoint;

AddItemizedOverlay itemizedOverlay;
OverlayItem overlayitem;

public void onCreate(Bundle savedInstanceState)
{
...

mapView = (MapView) findViewById(R.id.mapview);
mapView.setSatellite(false);
mapView.setBuiltInZoomControls(true);

mapOverlays = mapView.getOverlays();
mapOverlays.clear();
mapCtrl = mapView.getController();

drawCurrentUserLocation();
}

private void drawCurrentUserLocation()
{

// Drawable marker icon
Drawable drawable_user = this.getResources()
.getDrawable(R.drawable.mark_red);

itemizedOverlay = new AddItemizedOverlay(drawable_user, this);

overlayitem = new OverlayItem(mePoint, "Your Location","That is you!");

itemizedOverlay.addOverlay(overlayitem);

mapOverlays.add(itemizedOverlay);
itemizedOverlay.populateNow();
mapCtrl.animateTo(mePoint);
mapCtrl.setZoom(17);
}
}

AddItemizedOverlay.java

   public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {

private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();

private Context context;

public AddItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}

public AddItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}

@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{

if (event.getAction() == 1) {
GeoPoint geopoint = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
// latitude
double lat = geopoint.getLatitudeE6() / 1E6;
// longitude
double lon = geopoint.getLongitudeE6() / 1E6;

Geocoder geocoder = new Geocoder(context, Locale.getDefault());
String result = null;
try {
List<Address> list = geocoder.getFromLocation(lat, lon, 1);
if (list != null && list.size() > 0) {
Address address = list.get(0);
// sending back first address line and locality
result = address.getAddressLine(0) + ", " + address.getLocality();
Toast.makeText(context, result, Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
Log.e("Error", "Impossible to connect to Geocoder", e);
}

}
return false;
}

最佳答案

由于 myMapactivity 是 AddItemizedOverlay 的上下文,您可以使用:

((myMapactivity)context).setMarker(event) 在 AddItemizedOverlay 中;这将调用应在 myMapactivity 中实现的公共(public)方法 setMarker(MotionEvent event)。

关于Android MapActivity 和 ItemizedOverlay 在 TouchEvent 上改变当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12877259/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com