2013-02-09 88 views
0

我必須使用srceen中心的標記和移動地圖 而不是標記獲取標記顯示點的緯度。我有搜索互聯網的東西像拖動標記,但我是 不知道這是我需要什麼。任何解決方案?通過一個穩定的標記獲取緯度,經度?

+0

YOu可以收聽地圖movments並重新計算標記位置 – Mirco 2013-02-09 23:47:44

+0

在android中很難聽地圖的變化。 Android只有touchListeners。嘗試在地圖中心搜索繪製圓圈。我做了類似的事情。但是,我的觀點遲遲沒有跟上地圖中心。如果您對我的代碼感興趣,請在這種情況下告訴我。 – 2013-02-10 08:08:22

回答

0

在你的mapActivity wrtie這。

Location l = new Location(); 
// Location class will be used to calculate distance moved by map 
TimerTask tm; 
GeoPoint oldCenterOfMap, newCenteOfMap; 

//在您的恢復活動方法上執行oldCenterOfMap = mapView.getMapCenter();

Handler mHandler = new Handler(); 
int isTouched = 0; 
MapView mapview = (MapView) findViewById(R.id.yourmapview_from_xml); 

mapView.setOnTouchListener(new OnTouchListener() { 

@Override 
public boolean onTouch(View v, MotionEvent event) { 

if (event.getAction() == MotionEvent.ACTION_UP) { 
newCenteOfMap = mapView.getMapCenter(); 

if (mapchanged(oldCenterOfMap, newCenteOfMap)) { 
isTouched++; 

    // Here is what would you do when you lift your finger fromt he map***** 
newCenteOfMap =mapView.getMapCenter()); 

if (isTouched >= 1) { 
if (isTouched > 1) { 
mHandler.removeCallbacks(tm_circle); 
isTouched = 1; 
} 
mHandler.postDelayed(tm_circle, 1200); 
} 
} 
if (!mapchanged(oldCenterOfMap, newCenteOfMap)) 
mHandler.removeCallbacks(tm_circle); 
} 

return false; 
} 
}); 


tm_circle = new TimerTask() { 

@Override 
public void run() { 
isTouched = 0; 
// here is what you do after you touched a map moved after 1.2 seconds 
oldCenterOfMap = mapView.getMapCenter(); 

} 
}; 


public boolean mapchanged(GeoPoint oldCenter, GeoPoint newCenter) { 

    String distance = l.CalclauteDistance(oldCenter.getLongitudeE6()/1E6, 
      oldCenter.getLatitudeE6()/1E6, 
      newCenter.getLongitudeE6()/1E6, 
      newCenter.getLatitudeE6()/1E6); 

    if (Double.valueOf(distance) == 0.0) 
     return false; 
    else 
     return true; 
} 

以前的代碼會保持地圖移動的軌跡。 下一個代碼將在地圖移動的情況下做出響應。 在以上代碼中提供的註釋中(//這是您在1.2秒後移動的地圖後執行的操作和/ /以下是您從地圖中提起手指時的操作)關於我在另一篇文章上回答的指示。 這裏Adding multiple overlays on mapview dynamically

編輯

public String CalclauteDistance(double long1, double lat1, double long2, 
     double lat2) { 
    String Result; 
    Point p1 = new Point(long1, lat1); 
    Point p2 = new Point(long2, lat2); 
    Result = p1.distanceTo(p2) + ""; 
    return Result; 

} 

這裏是從位置類的功能(CalclauteDistance)。

+0

感謝您向我發送代碼,但我有一個問題。在「l.CalclauteDistance」這一點,它告訴我,這是類型位置未定義。我應該改變這種方法與其他? – Dimitris 2013-02-13 15:30:01

+0

您可以使用distanceTo或API中可用的任何其他方法來計算兩點之間的距離。但請確保它返回的數據類型。從現在起10個小時,我會用這種方法更新你。 – 2013-02-13 19:06:57

+0

對不起。這裏是CalclauteDistance()方法。 – 2013-02-15 10:12:28