我正在使用Mapbox和Android Studio IDE为Android做地图指南,但我很难处理自定义信息窗口。
我想给infowindow(我点击标记后的那个)充气但是到目前为止我想使用XML来进行简单的自定义(我愿意接受建议,我仍然需要添加不同的图像对于每个标记。)。我在代码本身中使用自定义仅用于测试,但我想为那个purmaorse夸大XML。
下面的图片展示了我为视图开发的原型,下一个图像显示了我实际从测试中获得的内容(代码在getInfoWindow函数中)我正在使用下面的代码进行开发:
Prototype in XML (Where as: ID-Translation-Type)
下面是我正在使用的地图代码的一部分,下一个代码显示了XML代码。
主要代码(Mapa.Java):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MapboxAccountManager.start(this,getString(R.string.access_token));
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap mapboxMap) {
IconFactory iconFactory = IconFactory.getInstance(Mapa.this);
Drawable iconDrawable = ContextCompat.getDrawable(Mapa.this, R.drawable.infoicon);
Icon icon = iconFactory.fromDrawable(iconDrawable);
mapboxMap.setInfoWindowAdapter(new MapboxMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(@NonNull Marker marker) {
View v = getLayoutInflater().inflate(R.layout.janela_infowindow, null);
TextView titulo = (TextView) v.findViewById(R.id.titulo);
TextView desc = (TextView) v.findViewById(R.id.descricao);
titulo.setText(marker.getTitle());
desc.setText(marker.getSnippet());
titulo.setTextColor(Color.WHITE);
desc.setTextColor(Color.WHITE);
v.setBackgroundColor(Color.rgb(27,77,62));
v.setPadding(10, 10, 10, 10);
return v;
}
});
mapboxMap.addMarker(new MarkerOptions()
.position(new LatLng(-15.76363, -47.86949))
.title("ICC Centro")
.snippet(getString(R.string.desc_ICC_Centro))
.icon(icon));
mapboxMap.addMarker(new MarkerOptions()
.position(new LatLng(-15.7629936, -47.86717415))
.title("Reitoria")
.snippet(getString(R.string.desc_Reitoria))
.icon(icon));
mapboxMap.addMarker(new MarkerOptions()
.position(new LatLng(-15.76196106, -47.87008166))
.title("FAU")
.snippet(getString(R.string.desc_FAU))
.icon(icon));
}
});
}
XML代码(janela_infowindow.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="200dp"
android:layout_height="270dp"
android:weightSum="1">
<ImageView
android:layout_width="200dp"
android:layout_height="0dp"
android:id="@+id/imagem"
android:adjustViewBounds="true"
android:layout_gravity="end"
android:contentDescription="@string/nome_local"
android:layout_weight="0.11" />
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="-30dp"
android:gravity="center"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/titulo"
android:typeface="serif"
android:textStyle="bold"/>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/descricao" />
编辑: 我不确定这是否正确,但每次我要设置高度,宽度或设置图像时,它会给我这个错误消息(设置高度后给出了此消息错误宽度):
FATAL EXCEPTION: main
Process: afinal.projeto.unb.guiadoaluno, PID: 8979
java.lang.NullPointerException: Attempt to write to field 'int android.view.ViewGroup$LayoutParams.height' on a null object reference
at afinal.projeto.unb.guiadoaluno.Mapa$1$1.getInfoWindow(Mapa.java:87)
at com.mapbox.mapboxsdk.annotations.Marker.showInfoWindow(Marker.java:165)
at com.mapbox.mapboxsdk.maps.MapboxMap.selectMarker(MapboxMap.java:1295)
at com.mapbox.mapboxsdk.maps.MapView$GestureListener.onSingleTapConfirmed(MapView.java:1792)
at android.view.GestureDetector$GestureHandler.handleMessage(GestureDetector.java:280)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5940)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
编辑2:好的,所以我对我无法在getInfoWindow中放置图像这一事实感到沮丧,无论我如何设置图像,它总是相同的错误信息。我知道可以设置图像,但只有当你在java文件中创建对象时,如果你想从膨胀的XML中更改图像,就不可能这样做。这是我得到的消息,无论我在java文件中声明,设置或使用图像:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
如果有人知道如何解决这个问题,我相信这会在Stack Overflow中解决我的问题。
答案 0 :(得分:0)
查看this example中找到的可能对您有所帮助的Mapbox Android Demo app。示例中找到的重要代码片段:
mapboxMap.setInfoWindowAdapter(new MapboxMap.InfoWindowAdapter() {
@Nullable
@Override
public View getInfoWindow(@NonNull Marker marker) {
// The info window layout is created dynamically, parent is the info window
// container
LinearLayout parent = new LinearLayout(CustomInfoWindowActivity.this);
parent.setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
parent.setOrientation(LinearLayout.VERTICAL);
// Depending on the marker title, the correct image source is used. If you
// have many markers using different images, extending Marker and
// baseMarkerOptions, adding additional options such as the image, might be
// a better choice.
ImageView countryFlagImage = new ImageView(CustomInfoWindowActivity.this);
switch (marker.getTitle()) {
case "spain":
countryFlagImage.setImageDrawable(ContextCompat.getDrawable(
CustomInfoWindowActivity.this, R.drawable.flag_of_spain));
break;
case "egypt":
countryFlagImage.setImageDrawable(ContextCompat.getDrawable(
CustomInfoWindowActivity.this, R.drawable.flag_of_egypt));
break;
default:
// By default all markers without a matching title will use the
// Germany flag
countryFlagImage.setImageDrawable(ContextCompat.getDrawable(
CustomInfoWindowActivity.this, R.drawable.flag_of_germany));
break;
}
// Set the size of the image
countryFlagImage.setLayoutParams(new android.view.ViewGroup.LayoutParams(150, 100));
// add the image view to the parent layout
parent.addView(countryFlagImage);
return parent;
}
});
}
});
}
如果您仍然有信息窗口问题,请告诉我。
编辑:我通过更改您提供的一些XML代码成功地夸大了您的布局:
<ImageView
android:id="@+id/imagem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:adjustViewBounds="true"/>
<TextView
android:id="@+id/titulo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold"
android:typeface="serif"/>
<TextView
android:id="@+id/descricao"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"/>
答案 1 :(得分:0)
所以,我发现了这个问题。就像Cammace说的那样,问题出现在XML中,不知何故有些属性如android:weightSum =“1”以及包含wrap_content的内容。
感谢Mapbox团队提供的帮助!