2014-08-31 52 views
0

在我的應用程序中,我希望用戶能夠提供他們想要在Google Map中使用的latlng。所以,首先,我已經創建了一個活動有一個按鈕,用戶打開谷歌地圖:標記在Google Maps API v2上不可拖動

private Button.OnClickListener opengmap = new Button.OnClickListener() { 
     public void onClick(View v) { 
     isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
     isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     if (!isGPSEnabled && !isNetworkEnabled) { 
      Toast.makeText(v.getContext(), v.getContext().getString(R.string.T1001_NoGPSDetection), Toast.LENGTH_LONG).show();     
     } else { 
      strprovider = locationManager.getBestProvider(criteria, false); 
      location = locationManager.getLastKnownLocation(strprovider); 
     } 

     **gmap = new GM001MapForAddress(v.getContext(), location.getLatitude(), location.getLongitude(), R.id.map); 
     gmap.initialize();** 

     } 
    }; 

然後,我有這樣的GM001MapForAddress初始化谷歌地圖,打算有拖動的標記:

public class GM001MapForAddress { 
public Context context; 
public GoogleMap googleMap; 
public MarkerOptions markeroption; 
public Marker marker; 
public CameraUpdate center; 


public Double latitude, longitude; 
public Integer id; 
public Activity activity; 

public GM001MapForAddress(Context context, Double latitude, Double longitude, Integer id) { 
    this.context = context; 
    this.latitude = latitude; 
    this.longitude = longitude; 
    this.id = id; 
} 

public void initialize() { 

    activity = (Activity) context; 
    googleMap = ((MapFragment) activity.getFragmentManager().findFragmentById(id)).getMap(); 
    googleMap.setOnMapClickListener(clickmap); 
    googleMap.setOnMarkerDragListener(dragmarker); 
    //googleMap.setMyLocationEnabled(true); 
    LatLng latlng = new LatLng(latitude, longitude); 
    marker = googleMap.addMarker(new MarkerOptions().position(latlng).draggable(true)); 
    if (marker.isDraggable()) { 
     System.out.println("HAHA " + latlng); 
    } 
    marker.setDraggable(true); 
    googleMap.setOnMarkerDragListener(dragmarker); 
    center= CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude)); 
    CameraUpdate zoom=CameraUpdateFactory.zoomTo(15); 
    googleMap.moveCamera(center); 
    googleMap.animateCamera(zoom); 
    googleMap.getFocusedBuilding(); 

} 

public OnMarkerDragListener dragmarker = new OnMarkerDragListener() { 

    @Override 
    public void onMarkerDrag(Marker marker) { 
     // TODO Auto-generated method stub 
     System.out.println("here1!!"); 

    } 

    @Override 
    public void onMarkerDragEnd(Marker arg0) { 
     // TODO Auto-generated method stub 
     System.out.println("here2!!"); 
     LatLng dragPosition = arg0.getPosition(); 
     double dragLat = dragPosition.latitude; 
     double dragLong = dragPosition.longitude; 
     System.out.println("halatlng: " + dragLat + " " + dragLong); 
     Log.i("info", "on drag end :" + dragLat + " dragLong :" + dragLong); 
     arg0.setPosition(dragPosition); 
     googleMap.addMarker(new MarkerOptions().position(dragPosition).draggable(true)); 

    } 

    @Override 
    public void onMarkerDragStart(Marker marker) { 
     // TODO Auto-generated method stub 
     System.out.println("here3!!"); 

    } 
}; 

public OnMapClickListener clickmap = new OnMapClickListener() { 

    @Override 
    public void onMapClick(LatLng point) { 
     // TODO Auto-generated method stub 
     googleMap.animateCamera(CameraUpdateFactory.newLatLng(point));   
     System.out.println("latdflng: " + point); 
    } 
}; 

我有我的clickmap偵聽器工作,但dragmarker偵聽器從不工作。整個週末我都在爲此工作(我在亞洲)。所以,希望有人能夠幫助我。

在此先感謝。

+0

你不能拖動標記,或者你只是不會在'dragmarker'中獲得回調? – Simas 2014-08-31 06:12:56

+0

嗨,我不能拖動標記 – kwytse 2014-08-31 08:03:16

回答

9

要啓用拖動模式,您需要長按一下標記,然後拖動。

如果標記很小,如果你有像我這樣笨拙的手指,可能很難點擊正確的點。

+0

忘了提及我在模擬器上測試,所以鼠標點擊應該能夠執行拖放操作。 – kwytse 2014-08-31 09:09:55

+1

@kwytse在模擬器上測試地圖是個不錯的主意。除此之外,你仍然需要用鼠標點擊鼠標。一秒鐘或兩秒鐘,不用移動鼠標即可開始拖動。 – Simas 2014-08-31 09:11:43

+0

你是對的。我多麼愚蠢!非常感謝! – kwytse 2014-08-31 09:26:16