我知道同一主题有一些问题,但它们都不适用于我的情况。我正在构建一个使用googleMap的应用程序,但我的问题是该片段正在创建两次,这会导致诸如放慢app并相互重叠等问题。这是程序的工作方式:主要活动有一个容器,它首先包含一个登录片段。如果登录成功,主活动将分离登录片段并添加地图片段。
public class MapFragment extends SupportMapFragment {
private GoogleMap mMap;
private Event mEventSelected;
private Person mPersonSelected;
private Map<Marker, Event> mMapMarkers;
private List<Polyline> mLines;
private TextView mPersonFullName;
private TextView mEventDescription;
private LinearLayout mMapEvent;
private ImageView mEventIcon;
private String mEventId;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mMapMarkers = new HashMap<>();
mLines = new ArrayList<>();
mEventSelected = null;
mPersonSelected = null;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_map, container, false);
v = drawDefaultLayout(v);
if (mMap == null)
{
initializeMap();
}
return v;
}
private void initializeMap() {
mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.google_map)).getMap();
if (getArguments() != null && getArguments().containsKey(FamilyMapData.getInstance().EVENT))
{
mEventId = getArguments().getString(FamilyMapData.getInstance().EVENT);
drawCenteredMap();
}
pinEvents();
markerHandler();
}
private void drawCenteredMap() {
mEventSelected = FamilyMapData.getEvents().get(mEventId);
setCurrentEvent();
CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(mEventSelected.getLatitude(), mEventSelected.getLongitude()));
mMap.moveCamera(center);
drawLines();
}
private void pinEvents() {
mMap.clear();
Map <String, Event> events = FamilyMapData.getEvents();
for (Event event : events.values())
{
LatLng coordinates = new LatLng(event.getLatitude(), event.getLongitude());
String description = event.getDescription();
if (Filter.sFilterMap.containsKey(description) && Filter.sFilterMap.get(description)) {
float color = getColor(description);
mMapMarkers.put(mMap.addMarker(new MarkerOptions().position(coordinates).icon(BitmapDescriptorFactory.defaultMarker(color))), event);
}
}
}
这是我的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:layout_width="wrap_content"
android:layout_height="0dp"
class="com.google.android.gms.maps.SupportMapFragment"
android:tag="@string/google_map"
android:id="@+id/google_map"
android:layout_weight="5"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:clickable="true"
android:baselineAligned="false"
android:id="@+id/map_event">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/icon_event_divisor">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:clickable="false"
android:layout_gravity="center"
android:id="@+id/event_icon"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/person_full_name"
android:textSize="10dp"
android:id="@id/person_full_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="8dp"
android:text="@string/event_description"
android:layout_below="@id/person_full_name"
android:id="@+id/event_description"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
你能帮我弄清楚它为什么会发生,所以我可以解决它吗?提前谢谢。
答案 0 :(得分:0)
如果你想在原始地图片段上添加额外的工作,你的片段必须扩展MapFragment,如果你想要它的回调:
import com.google.android.gms.maps.MapFragment;
public class MapFrag extends MapFragment implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,
GoogleMap.OnInfoWindowClickListener,
GoogleMap.OnMapLongClickListener,
GoogleMap.OnMapClickListener,
GoogleMap.OnMarkerClickListener{
然后删除onCreateView
中的inflate部分,然后更改xml布局中的类标记以解决此类:
fragment
android:layout_width="wrap_content"
android:layout_height="0dp"
class="yourPackage.MapFrag"
android:tag="@string/google_map"
android:id="@+id/google_map"
android:layout_weight="5"/>
然后在片段类中使用getMap()
方法访问地图实例并继续