我陷入困境。我有一个自定义列表适配器的自定义列表。每个列表项都有一个按钮,如果我点击这个按钮弹出一个对话框,弹出应该包含谷歌地图(后面带标记)。我试图将片段分配给SupportMapFragment,但Android Studio告诉我,“getSupportFragmentManager()无法解析方法”。我的问题是如何将这个地图(片段)放入对话框?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="hu.bognaroliver.bajaintezemnyek.CustomListAdapter">
<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_alignParentTop="true" />
<fragment
android:id="@+id/map"
android:layout_alignParentBottom="true"
android:layout_below="@+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
</RelativeLayout>
这是地图。
package hu.bognaroliver.bajaintezemnyek;
import java.util.ArrayList;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.CameraUpdateFactory;
public class CustomListAdapter extends ArrayAdapter<Institute> implements OnMapReadyCallback
{
private LayoutInflater inflater;
private ImageView instituteLogo;
private TextView instituteName;
private TextView instituteAddress;
private TextView instituteCloseTime;
private ImageView instituteType;
private TextView instituteStatus;
private ImageView mapView;
private Dialog mDialog;
private final Context context = getContext();
public CustomListAdapter(@NonNull Context context, ArrayList<Institute> item)
{
super(context, 0, item);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
{
Institute institute = getItem(position);
if(convertView == null)
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.institute_item, parent, false);
instituteLogo = (ImageView) convertView.findViewById(R.id.instituteLogo);
instituteName = (TextView) convertView.findViewById(R.id.instituteName);
instituteAddress = (TextView) convertView.findViewById(R.id.instituteAddress);
instituteCloseTime = (TextView) convertView.findViewById(R.id.instituteCloseTime);
instituteType = (ImageView) convertView.findViewById(R.id.instituteType);
instituteStatus = (TextView) convertView.findViewById(R.id.instituteStatus);
mapView = (ImageView) convertView.findViewById(R.id.mapView);
instituteLogo.setImageResource(R.drawable.cba_logo);
instituteName.setText(institute.getName());
instituteAddress.setText(institute.getAddress());
instituteCloseTime.setText("30 perc múlva");
instituteType.setImageResource(R.drawable.ic_shopping_cart_black_48dp);
instituteStatus.setText(institute.getStatus().toString());
mapView.setImageResource(R.drawable.map_icon);
SupportMapFragment supportMapFragment = (SupoortMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
//MapFragment mapFragment = ((MapFragment) findFragmentById(R.id.map));
//mapFragment.getMapAsync(this);
mDialog = new Dialog(context);
mDialog.setContentView(R.layout.custom_dialog_map);
mDialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) mDialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
mapView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
mDialog.show();
}
});
return convertView;
}
@Override
public void onMapReady(GoogleMap map)
{
//DO WHATEVER YOU WANT WITH GOOGLEMAP
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.setTrafficEnabled(false);
map.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(46.1811211,18.9542073) , 13.0f) );
map.setIndoorEnabled(true);
map.setBuildingsEnabled(true);
map.getUiSettings().setZoomControlsEnabled(true);
}
}
感谢您的帮助:)
答案 0 :(得分:0)
mMap.addMarker(new MarkerOptions()
.position(lng)
.title("Marker Title"));
mMap.setOnMarkerClickListener(new OnMarkerClickListener()
{
@Override
public boolean onMarkerClick(Marker arg0) {
// Marker source is clicked
return true;
}
});
答案 1 :(得分:0)
使用Google MapView
执行此操作的快捷方式 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="hu.bognaroliver.bajaintezemnyek.CustomListAdapter">
<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_alignParentTop="true" />
<com.google.android.gms.maps.MapView
android:id="@+id/map_view"
android:layout_below="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.google.android.gms.maps.MapView>
<Button
android:id="@+id/btn_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ok"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
<Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ok"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"/>
</RelativeLayout>
在itemclick显示对话框
new Dialog(getContext()) {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
MapView map = findViewById(R.id.map_view);
map.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
//add marker here
googleMap.addMarker(new MarkerOptions()
.position(lng)
.title("Marker Title"));
googleMap.setOnMarkerClickListener(
new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker arg0) {
// Marker source is clicked
return true;
}
});
}
});
findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do something
}
});
findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
}.show();
尝试这个
Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialogmap);
dialog.show();
GoogleMap googleMap;
MapView mMapView = (MapView) dialog.findViewById(R.id.mapView);
MapsInitializer.initialize(getActivity());
mMapView = (MapView) dialog.findViewById(R.id.mapView);
mMapView.onCreate(dialog.onSaveInstanceState());
mMapView.onResume();// needed to get the map to display immediately
googleMap = mMapView.getMap();