精華區beta Android 關於我們 聯絡資訊
一般的在Google Map上的應用,多半都是GeoPoint的座標轉成螢幕的Pixels座標,然後畫 圖在螢幕上,可是今天我想要反過來,就是我用手去碰觸螢幕上的地圖,然後抓出地圖上 被我碰到的點的座標是多少,範例如下: (這個之前有po過,這次po的是改良版,加了myItemOverlay的置中圖示,方便辨認) http://www.anddev.org/viewtopic.php?p=28140 OnTouchListener myOnTouchListener=new OnTouchListener(){ public boolean onTouch(View mapView, MotionEvent m){ int action=m.getAction(); switch(action){ case MotionEvent.ACTION_UP: int x=(int)m.getX(); int y=(int)m.getY(); double latspan=(mMapView01.getLatitudeSpan()/1.0E6); double lngspan=(mMapView01.getLongitudeSpan()/1.0E6); GeoPoint center=mMapView01.getMapCenter(); double xpcent=(x/(double)mMapView01.getWidth()); double ypcent=(y/(double)mMapView01.getHeight()); double lat0=(center.getLatitudeE6()/1.0E6+(latspan/2.0)); double lng0=(center.getLongitudeE6()/1.0E6-(lngspan/2.0)); lat=lat0-(ypcent*latspan); lng=lng0+(xpcent*lngspan); //Log.i("x=",""+x); //Log.i("y=",""+y); GeoPoint newpoint=new GeoPoint((int)(lat*1E6),(int)(lng*1E6)); // mMapView01.getController().zoomOut(); /*Log.i("Map","Height"+mMapView01.getHeight()); Log.i("Map","width"+mMapView01.getWidth()); Log.i("Current","LatitudeSpane"+mMapView01.getLatitudeSpan()); Log.i("Current","LongitudeSpan"+mMapView01.getLongitudeSpan()); Log.i("center Longitude","is"+newpoint.getLongitudeE6()); Log.i("center","Latitude is"+newpoint.getLatitudeE6()); Log.i("new ","latitude "+lat); Log.i("new","longitude "+lng); Log.i("background","is"+mMapView01.getBackground()); Log.i("Zoom in","works"); */ //mMapView01.invalidate(); mMapController01.animateTo(newpoint); //mMapController01.zoomIn(); GeoPoint gp = null; gp = new GeoPoint((int)(lat*1E6),(int)(lng*1E6)); //在地圖上加icon Drawable dr = getResources().getDrawable(R.drawable.star); dr.setBounds(-15, -15, 15, 15); MyItemOverlay mOverlay01= new MyItemOverlay(dr, gp); List<Overlay> overlays= mMapView01.getOverlays(); overlays.clear(); overlays.add(mOverlay01); break; } return false; } }; mMapView01.setOnTouchListener(myOnTouchListener); public class MyItemOverlay extends ItemizedOverlay<OverlayItem>{ private List<OverlayItem> items = new ArrayList<OverlayItem>(); public MyItemOverlay(Drawable arg0, GeoPoint gp) { super(arg0); // TODO Auto-generated constructor stub items.add(new OverlayItem(gp, "Title", "Snippet")); populate(); } @Override protected OverlayItem createItem(int i) { // TODO Auto-generated method stub //return null; return items.get(i); } @Override public int size() { // TODO Auto-generated method stub //return 0; return items.size(); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.114.78.145 ※ 編輯: meya 來自: 140.114.78.145 (07/26 22:33)