私のAndroidアプリケーションにはグーグルマップv2があり、いくつかのマーカーがあります。ユーザーがこれらのマーカーの1つをクリックすると、タイトルポップアップが表示されます。ユーザーがクリックせずにこれらのタイトルを常に表示するにはどうすればよいですか?
6 に答える
だけお電話Marker.showInfoWindow();
ください。https://developers.google.com/maps/documentation/android/marker#info_windowsおよびhttps://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/modelを参照してください/ Marker#showInfoWindow()
そんな感じ:
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title("Your position")).showInfoWindow();
あなたはこれを行うことができます:
Marker marker = mMap.addMarker(new MarkerOptions().position(currentPosition).title("Your text"));
marker.showInfoWindow();
マーカーの周りの地図にマーカーのタイトルを表示するために、インターネットで見つけた解決策はどれもうまくいきませんでした。そこで私は袖をまくり上げて、この問題を解決するこのライブラリを作成しました。
https://github.com/androidseb/android-google-maps-floating-marker-titles
これがどのように機能するかのプレビューです:
私はいくつかの実装ではあまり得意ではありませんが、ここに私のコードがあります:
public BitmapDescriptor getBitmapFromView(String title) {
View view = LayoutInflater.from(activity.getApplicationContext()).inflate(R.layout.marker_tooltip, null);
((TextView) view.findViewById(R.id.tooltips_title)).setText(title);
//Get the dimensions of the view. In my case they are in a dimen file
int width = activity.getResources().getDimensionPixelSize(R.dimen.tooltips_width);
int height = activity.getResources().getDimensionPixelSize(R.dimen.tooltips_height);
int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
view.measure(measuredWidth, measuredHeight);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return BitmapDescriptorFactory.fromBitmap(bitmap);
}
...
marker.setIcon(getBitmapFromView(marker.getTitle()));
...
これをマップアプリのフラグメントにいくつかのバリエーションを付けて実装しました。たとえば、カスタム情報ウィンドウとマップ上のいくつかのイベントがありますが、その実装では、すべてのマーカータイトルを同時に表示する必要がありました。それがあなたにも役立ったかどうか教えてください
「activity.getResources()。getDimensionPixelSize(R.dimen.tooltips_width)」変数のACTIVITYは、フラグメント内のプロパティです。
多分私はこれに答えるのに少し遅れていますが、以下のコードは私のために働きます:
//このライブラリを追加します
implementation 'com.google.maps.android:android-maps-utils:0.5'
//次に以下の方法を使用します
public void makersMaker(GoogleMap googleMap){
IconGenerator iconFactory = new IconGenerator(this);
Marker marker1 = googleMap.addMarker(new MarkerOptions().position(newLatLng(-1.3177336,36.8251902));
marker1.setIcon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon("Marker 1")));
Marker marker2 = googleMap.addMarker(new MarkerOptions().position(new LatLng(-1.2857399,36.8214088)));
marker2.setIcon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon("Marker 2")));
}