2017-12-01 20 views
0

他のフラグメントやアクティビティでマップを表示するためにカスタムビューを作成しました。 しかし、Googleマップのようなフレームで簡単なクリックを取得したいと思います。 誰も私を助けることができますか?マップ内のonClickListenerを追加CustomView

は、カスタムビューがあります:私はXMLで使用する方法です

public class MapView extends FrameLayout { 

    private Subject<GoogleMap> mapSubject; 

    private static final float MAP_ZOOM_LEVEL_STATIC = 12.0f; 

    private Circle mCircle; 

    public MapView(@NonNull Context context) { 
    super(context); 
    init(context, null); 
    } 

    public MapView(@NonNull Context context, @Nullable AttributeSet attrs) { 
    super(context, attrs); 
    init(context, attrs); 
    } 

    public MapView(@NonNull Context context, @Nullable AttributeSet attrs, 
      @AttrRes int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    init(context, attrs); 
    } 

    private void init(Context context, AttributeSet attrs) { 
    SupportMapFragment mapFragment = SupportMapFragment.newInstance(); 

    if (!isInEditMode()) { 
     FragmentTransaction fragmentTransaction =((AppCompatActivity)context) 
     .getSupportFragmentManager().beginTransaction(); 
     fragmentTransaction.add(getId(), mapFragment); 
     fragmentTransaction.commit(); 

     mapSubject = BehaviorSubject.create(); 
     Observable.create((ObservableOnSubscribe<GoogleMap>) e -> mapFragment.getMapAsync(e::onNext)) 
     .subscribe(mapSubject); 
     mapSubject.subscribe(googleMap -> mCircle = googleMap.addCircle(new 
     CircleOptions().center(new LatLng(0.0f, 0.0f)).radius(0))); 
    } 
    } 


    /** 
    * Update position and create a new Circle and move camera to new position 
    * 
    * @param location The location to be updated. 
    */ 
    public void updatePosition(LatLng location) { 
    mapSubject.subscribe(googleMap -> { 
     mCircle.remove(); 
     mCircle = googleMap.addCircle(new 
     CircleOptions().center(location).radius(15) 
     .strokeWidth(10) 
     .strokeColor(Color.RED) 
     .fillColor(Color.RED)); 

     googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 
     MAP_ZOOM_LEVEL_STATIC)); 
     }); 
    } 

    /** 
    * This method is responsible to add a polyline in google maps 
    * 
    * @param results array with directions 
    */ 
    public void addPolyline(DirectionsResult results) { 
    if(results != null) { 
     mapSubject.subscribe(googleMap -> { 
     List<LatLng> decodedPath = 
      PolyUtil.decode(results.routes[0] 
      .overviewPolyline 
      .getEncodedPath()); 

     googleMap.addPolyline(new 
      PolylineOptions() 
      .addAll(decodedPath) 
      .width(10) 
      .color(Color.RED) 
      .geodesic(true)); 

     updatePosition(decodedPath.get(0)); 
     }); 
    } 
    } 
} 

:デバッガでここに合格しなかったので、私は、成功せず(クリックを特定しようとしている方法を

<com.name.of.package.custom_view.MapView 
    android:id="@+id/mapView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

ザッツ):

MapView mapView = (MapView)findViewById(R.id.mapView) 
mapView.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
    toogle(view); 
    Toast.makeText(mContext, "Map clicked!", Toast.LENGTH_SHORT).show(); 
    } 
}); 
+0

あなたはアンドロイドを使用してみました:clickeable = "true" を? – diegoveloper

+0

私はtryedしかしdidntの仕事... –

答えて

1

についての検索、私は、マップ内の単純なクリックを確認するための方法を見つけます。あなたがGoogleMap.OnMapClickListenerを実装する必要がthisを置くため

mapView.setOnMapClick(this); 

PSを.::あなたはたとえば、MapViewのを使用する場合

、その後

/** 
* This method is responsible to add a MapClickListener to handle simple clicks in map. 
* 
* @param mapClickListener class that will handle simple click 
*/ 
public void setOnMapClick(GoogleMap.OnMapClickListener mapClickListener) { 
    mapSubject.subscribe(googleMap -> googleMap.setOnMapClickListener(mapClickListener)); 
} 

MapView.javaで関数を作成し、リスナーを設定しますマップビューを使用しているアクティビティ/フラグメント。

した後、あなたのフラグメント/アクティビティでオーバーライドメソッドを入れて、実装:

@Override 
public void onMapClick(LatLng latLng) { 
    if (Logger.DEBUG) Log.d(TAG, "onMapClick"); 
    // Do something 
} 
関連する問題