53

マーカーがクリックされると、カメラのデフォルトの動作は画面の中央に表示されますが、通常は情報ウィンドウに長いテキストの説明があるため、実際にカメラの位置を変更してマーカーが画面の下部にくるようにする方が便利です。画面(画面中央に情報ウィンドウを作成)。以下のようにonMarkerClick関数をオーバーライドすることでそれができるはずだと思います(この関数がtrueを返すと、デフォルトの動作はキャンセルされます)

@Override

public boolean onMarkerClick(final Marker marker) {


    // Google sample code comment : We return false to indicate that we have not

    // consumed the event and that we wish
    // for the default behavior to occur (which is for the camera to move
    // such that the
    // marker is centered and for the marker's info window to open, if it
    // has one).

    marker.showInfoWindow();

            CameraUpdate center=
                CameraUpdateFactory.newLatLng(new LatLng(XXXX,
                                                         XXXX));
            mMap.moveCamera(center);//my question is how to get this center

            // return false;
    return true;
}

編集:

受け入れられた回答の手順、以下のコードを使用して解決された問題:

@Override

    public boolean onMarkerClick(final Marker marker) {

                //get the map container height
        LinearLayout mapContainer = (LinearLayout) findViewById(R.id.map_container);
        container_height = mapContainer.getHeight();

        Projection projection = mMap.getProjection();

        LatLng markerLatLng = new LatLng(marker.getPosition().latitude,
                marker.getPosition().longitude);
        Point markerScreenPosition = projection.toScreenLocation(markerLatLng);
        Point pointHalfScreenAbove = new Point(markerScreenPosition.x,
                markerScreenPosition.y - (container_height / 2));

        LatLng aboveMarkerLatLng = projection
                .fromScreenLocation(pointHalfScreenAbove);

        marker.showInfoWindow();
        CameraUpdate center = CameraUpdateFactory.newLatLng(aboveMarkerLatLng);
        mMap.moveCamera(center);
        return true;



    }

助けてくれてありがとう^ ^

4

10 に答える 10

5

マップのズームインを気にせず、マーカーを下に表示したいだけの場合は、以下を参照してください。それはより簡単な解決策だと思います

double center = mMap.getCameraPosition().target.latitude;
double southMap = mMap.getProjection().getVisibleRegion().latLngBounds.southwest.latitude;

double diff = (center - southMap);

double newLat = marker.getPosition().latitude + diff;

CameraUpdate centerCam = CameraUpdateFactory.newLatLng(new LatLng(newLat, marker.getPosition().longitude));

mMap.animateCamera(centerCam);
于 2016-09-25T05:45:11.000 に答える
1

少し調べてみたところ、ドキュメントによると、マップは正方形で、ズーム レベルがゼロの場合、幅と高さは 256 dp で、北緯 85 度です。マップの幅はズーム レベルに応じて増加し、幅と高さ = 256 * 2N dp になります。N はズーム レベルです。したがって、理論的には、マップの高さを取得し、それを合計 170 度で割って 1 度あたりの dp を取得することで、新しい場所を決定できます。次に、画面の高さ (またはマップビューの高さ) を 2 で割った dp で取得し、ビュー サイズの半分を緯度に変換します。次に、新しいカメラ ポイントを南緯の度数に設定します。必要に応じてコードを追加できますが、現在電話中です。

于 2013-05-26T22:18:02.803 に答える
1

I also faced this problem and fixed it in a hacky way. Let's declare a double field first. You need to adjust the value of it based on your requirement but I recommend you keep it between 0.001~0.009 otherwise you can miss your marker after the zoom animation.

  double offset = 0.009 
  /*You can change it based on your requirement.
 For left-right alignment please kindly keep it between 0.001~0.005 */

For bottom-centered:

    LatLng camera = new LatLng(marker.getPosition().latitude+offset , marker.getPosition().longitude);
//Here "marker" is your target market on which you want to focus

For top-centered:

LatLng camera = new LatLng(marker.getPosition().latitude-offset , marker.getPosition().longitude);

For left-centered:

LatLng camera = new LatLng(marker.getPosition().latitude, marker.getPosition().longitude+offset);

For right-centered:

LatLng camera = new LatLng(marker.getPosition().latitude-offset , marker.getPosition().longitude-offset);

Then finally call the -

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(camera, yourZoom));
于 2020-08-14T07:37:41.890 に答える
0

ここで提案されているすべてのソリューションを試し、それらを組み合わせて実装しました。地図の投影、傾斜、ズーム、情報ウィンドウの高さを考慮してください。

「カメラビュー」の下部にマーカーを実際に配置するわけではありませんが、ほとんどの場合、情報ウィンドウとマーカーの中心にうまく対応していると思います.

@Override
public boolean onMarkerClick(Marker marker) {
    mIsMarkerClick = true;
    mHandler.removeCallbacks(mUpdateTimeTask);
    mLoadTask.cancel(true);
    getActivity().setProgressBarIndeterminateVisibility(false);
    marker.showInfoWindow();
    Projection projection = getMap().getProjection();
    Point marketCenter = projection.toScreenLocation(marker.getPosition());
    float tiltFactor = (90 - getMap().getCameraPosition().tilt) / 90;
    marketCenter.y -= mInfoWindowAdapter.getInfoWindowHeight() / 2 * tiltFactor;
    LatLng fixLatLng = projection.fromScreenLocation(marketCenter);

    mMap.animateCamera(CameraUpdateFactory.newLatLng(fixLatLng), null);

    return true;
}

次に、カスタム アダプターは、その高さを取得できるように、情報ウィンドウの膨張したビューのインスタンスを保持する必要があります。

public int getInfoWindowHeight(){
    if (mLastInfoWindoView != null){
        return mLastInfoWindoView.getMeasuredHeight();
    }
    return 0;
}
于 2013-08-13T10:32:56.460 に答える
0

いくつかの経験の後、私は自分にとって良い解決策を実装しました。

DisplayMetrics metrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(metrics);

Point targetPoint = new Point(metrics.widthPixels / 2, metrics.heightPixels - metrics.heightPixels / 9);
LatLng targetLatlng = map.getProjection().fromScreenLocation(targetPoint);
double fromCenterToTarget = SphericalUtil.computeDistanceBetween(map.getCameraPosition().target, targetLatlng);

LatLng center = SphericalUtil.computeOffset(new LatLng(location.latitude, location.longitude), fromCenterToTarget/1.2, location.bearing);
CameraUpdate camera = CameraUpdateFactory.newLatLng(center);
map.animateCamera(camera, 1000, null);

ここ。まず、マーカーを移動する画面上の物理的なポイントを選択します。次に、LatLng に変換します。次のステップ - 現在のマーカー位置 (中央) からターゲットまでの距離を計算します。最後に、マップの中心をマーカーから計算された距離にまっすぐ移動します。

于 2017-08-18T04:02:41.743 に答える
0

位置座標に従ってカメラを中心に配置しようとしている人

CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(Lat, Lon))
        .zoom(15) 
        .bearing(0)
        .tilt(45)
        .build();
    map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

クレジット

于 2016-06-15T02:09:09.420 に答える