gpt4 book ai didi

android - 通过点击 map 获取坐标(openstreetmaps)

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:18:45 26 4
gpt4 key购买 nike

我们如何通过在打开的街道 map 中点击某个点来获取该点的坐标?

尝试过:

public void onClick(View v) {
Projection proj = mapView.getProjection();
IGeoPoint p = proj.fromPixels(v.getX(), v.getY());
System.out.println("x: "+ v.getX() + " y: "+ v.getY());
}

干杯,塔纳西奥

最佳答案

这是我自己实现的 MapView,用于获取点击 map 的位置。

public class MapViewLoc extends MapView {

private Overlay tapOverlay;
private OnTapListener onTapListener;

protected MapViewLoc(Context context, int tileSizePixels, ResourceProxy resourceProxy, MapTileProviderBase tileProvider, Handler tileRequestCompleteHandler, AttributeSet attrs) {
super(context, tileSizePixels, resourceProxy, tileProvider, tileRequestCompleteHandler, attrs);
}

public MapViewLoc(Context context, AttributeSet attrs) {
super(context, attrs);
}

public MapViewLoc(Context context, int tileSizePixels) {
super(context, tileSizePixels);
}

public MapViewLoc(Context context, int tileSizePixels, ResourceProxy resourceProxy) {
super(context, tileSizePixels, resourceProxy);
}

public MapViewLoc(Context context, int tileSizePixels, ResourceProxy resourceProxy, MapTileProviderBase aTileProvider) {
super(context, tileSizePixels, resourceProxy, aTileProvider);
}

public MapViewLoc(Context context, int tileSizePixels, ResourceProxy resourceProxy, MapTileProviderBase aTileProvider, Handler tileRequestCompleteHandler) {
super(context, tileSizePixels, resourceProxy, aTileProvider, tileRequestCompleteHandler);
}

private void prepareTagOverlay(){

this.tapOverlay = new Overlay(this.getContext()) {

@Override
protected void draw(Canvas c, MapView osmv, boolean shadow) {

}

@Override
public boolean onSingleTapConfirmed(MotionEvent e, MapView mapView) {

Projection proj = mapView.getProjection();
GeoPoint p = (GeoPoint) proj.fromPixels((int) e.getX(), (int) e.getY());
proj = mapView.getProjection();

final GeoPoint geoPoint = (GeoPoint) proj.fromPixels((int) e.getX(), (int) e.getY());

if(MapViewLoc.this.onTapListener != null){

MapViewLoc.this.onTapListener.onMapTapped(geoPoint);

Location location = new Location("");
location.setLatitude((double) geoPoint.getLatitudeE6() / 1000000);
location.setLongitude((double) geoPoint.getLongitudeE6() / 1000000);
location.setAccuracy(Criteria.ACCURACY_FINE);

MapViewLoc.this.onTapListener.onMapTapped(location);
}

return true;
}
};
}

public void addTapListener(OnTapListener onTapListener){

this.prepareTagOverlay();

this.getOverlays().add(0, this.tapOverlay);

this.onTapListener = onTapListener;
}

public void removeTapListener(){

if(this.tapOverlay != null && this.getOverlays().size() > 0){

this.getOverlays().remove(0);
}

this.tapOverlay = null;
this.onTapListener = null;
}

public interface OnTapListener{

void onMapTapped(GeoPoint geoPoint);

void onMapTapped(Location location);

}

}

要获取位置,只需设置接口(interface)OnTapListener。

mapView.addTapListener(new MapViewLoc.OnTapListener() {

@Override
public void onMapTapped(GeoPoint geoPoint) {}

@Override
public void onMapTapped(Location location) {

Toast toast = Toast.makeText(getApplicationContext(),
"Latitude: " + location.getLatitude() + " Longitude: " + location.getLongitude(),
Toast.LENGTH_SHORT);

toast.show();
}
});

关于android - 通过点击 map 获取坐标(openstreetmaps),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16665426/

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