gpt4 book ai didi

android - 根据变量更改谷歌地图标记信息窗口中的图像

转载 作者:行者123 更新时间:2023-11-29 19:19:01 25 4
gpt4 key购买 nike

对于一个项目,我想使用 CSV 文件来添加标记。信息窗口中的内容基于 CSV 文件,如下所示:

x_coordinate          y_coordinate         title       description
52.3494703659177 4.63031105906557 m_1 beautifull
52.3494703659177 4.63031105906557 m_2 Awfull

此时我可以将标记添加到 map 并将标题和描述添加到信息窗口,但我无法添加基于 CSV 文件的图像。图像的名称与 id 相同。因此,如果标题是 m_1,我想将名称为 m_1 的图像添加到信息窗口。

虽然我可以覆盖图像的现有来源,但这不起作用,因为当我按下应用程序中的一个标记时,应用程序崩溃了。代码如下:

public void onMapReady(GoogleMap googlemap) {
mMap = googlemap;

if (mMap != null) {
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
View v = getLayoutInflater().inflate(R.layout.infow_window, null);

TextView tvHaarlem = (TextView) v.findViewById(R.id.tvHaarlem);
TextView tvSnippet = (TextView) v.findViewById(R.id.tvNoord_Holland);
String image_id = new String(marker.getTitle());
int resID = getResources().getIdentifier(image_id, "drawable", "package.name");
ImageView img= (ImageView) findViewById(R.id.map_image);
img.setImageResource(R.drawable.m_1);

LatLng ll = marker.getPosition();
tvHaarlem.setText(marker.getTitle());
tvSnippet.setText(marker.getSnippet());

return v;
}

@Override
public View getInfoContents(Marker marker) {
return null;
}

});

mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
Intent intent = new Intent(maps.this, MainActivity.class);
startActivity(intent);
}
});
}



InputStream instream = getResources().openRawResource(R.raw.points_in_map);
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader reader = new BufferedReader(inputreader);
List<LatLng> latLngList = new ArrayList<LatLng>();
String line = "";

try {
while ((line = reader.readLine()) != null) // Read until end of file
{
double lat = Double.parseDouble(line.split(",")[1]);
double lon = Double.parseDouble(line.split(",")[2]);
String title_marker = new String(line.split(",")[3]);
String description_marker = new String(line.split(",")[4]);
LatLng pos = new LatLng(lat, lon);

//coordinates too map
mMap.addMarker(new MarkerOptions()
.title(String.valueOf(title_marker))
.snippet(String.valueOf(description_marker))
.position(pos));

}
} catch (IOException e) {
e.printStackTrace();
}
}


}

信息窗口的xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
>

<ImageView
android:id="@+id/map_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_welgelegen"/>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:onClick="sendmessage">

<TextView
android:id="@+id/tvHaarlem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold" />

<TextView
android:id="@+id/tvNoord_Holland"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"/>

</LinearLayout>

</LinearLayout>

加上这一点我完全不知道该怎么做。我还尝试在信息窗口中创建一个新的 ImageView ,但这也不起作用。

我在 log cat 中得到的错误是:

03-17 13:48:58.097 9689-9689/com.diovae.berend.layout_legend E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.diovae.berend.layout_legend, PID: 9689
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
at com.diovae.berend.layout_legend.maps$1.getInfoWindow(maps.java:63)
at com.google.android.gms.maps.GoogleMap$7.zzh(Unknown Source)
at com.google.android.gms.maps.internal.zzd$zza.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:499)
at com.google.android.gms.maps.internal.p.a(:com.google.android.gms.DynamiteModulesB:94)
at com.google.maps.api.android.lib6.impl.by.a(:com.google.android.gms.DynamiteModulesB:89)
at com.google.maps.api.android.lib6.impl.by.a(:com.google.android.gms.DynamiteModulesB:124)
at com.google.maps.api.android.lib6.gmm6.api.e.a(:com.google.android.gms.DynamiteModulesB:188)
at com.google.maps.api.android.lib6.gmm6.api.g.c(:com.google.android.gms.DynamiteModulesB:200)
at com.google.maps.api.android.lib6.impl.db.g(:com.google.android.gms.DynamiteModulesB:23225)
at com.google.maps.api.android.lib6.impl.dd.b(:com.google.android.gms.DynamiteModulesB:304)
at com.google.maps.api.android.lib6.gmm6.api.e.a(:com.google.android.gms.DynamiteModulesB:242)
at com.google.maps.api.android.lib6.gmm6.vector.m.a(:com.google.android.gms.DynamiteModulesB:4070)
at com.google.maps.api.android.lib6.gmm6.vector.af.c(:com.google.android.gms.DynamiteModulesB:611)
at com.google.maps.api.android.lib6.gmm6.vector.df.onSingleTapConfirmed(:com.google.android.gms.DynamiteModulesB:236)
at com.google.maps.api.android.lib6.impl.gesture.g.onSingleTapConfirmed(:com.google.android.gms.DynamiteModulesB:189)
at com.google.maps.api.android.lib6.impl.gesture.i.handleMessage(:com.google.android.gms.DynamiteModulesB:132)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

最佳答案

在您的 getInfoWindow 方法中替换

ImageView img= (ImageView) findViewById(R.id.map_image);

用这个:

ImageView img= (ImageView) v.findViewById(R.id.map_image);

关于android - 根据变量更改谷歌地图标记信息窗口中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42857853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com