双 Android Map/Google 地点详细信息屏幕

Dual Android Map/Google Places Details Screen

我在使用片段时遇到了一些真正的麻烦!我有一个结果 Activity class 运行 有两个片段,下半部分地图带有位置和附近 google 放置标记,上半部分将(或者我也想要它但是它不会红润的工作)保存地址和详细信息也是用户 selected 标记。我得到了一切,我可以 select 标记!我可以加载我的位置,我可以获得最近的地点,我什至可以 运行 将地点详细信息添加到另一个片段(但它会在屏幕外加载)。但是你认为我可以将细节加载到屏幕的上半部分吗!! (不!)我需要帮助,我几乎为此挠头了一段时间……它是 20 到 2,我不能再面对它的另一天……而且我知道它真的很小(当我问的时候总是这样)在堆栈上),但我无法发现它。你能帮帮我吗!
xml 包含片段的代码,map_fragment,左侧 search_frag 用于动态

 <RelativeLayout
    android:id="@+id/search_frag"
    android:layout_width="match_parent"
    android:layout_height="@dimen/results"
    android:background="@color/cream"
    android:layout_below="@+id/top_banner"
    android:orientation="horizontal"
    tools:layout="@layout/result_hint">
    </RelativeLayout>
<fragment
    android:layout_below="@id/search_frag"
    android:id="@+id/map_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/cream"
    android:orientation="horizontal"
    android:name="boo.thefoodhunt.fragment.MapFragment"
    tools:layout="@layout/template_map_view"/>

ResultsActivitiy.java:我把一个虚拟片段放在那里看它是否会加载并且它确实加载了,认为用新片段替换它可能更容易(不是)

public class ResultsActivity extends FragmentActivity
        implements MapFragment.MarkerClickCB {
    //Log
    private final String TAG = this.getClass().getName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_results);
    //Master data list Fragment

        FragmentManager fm = getSupportFragmentManager();

        SelectFrag firstFragment = new SelectFrag();

        FragmentTransaction ft = fm.beginTransaction();

        ft.add(R.id.search_frag, firstFragment);
        ft.commit();
}

MapFragment.java public boolean onMarkerClick(Marker marker) 上的 else if 语句的一部分。未注释的代码工作并传达 google 位置详细信息用户 selects,地址并将详细信息发送到全屏片段。

 if (marker.equals(this.myLocMarker) && nearby != null) {
            for (Marker placeMarker : nearby.keySet()) {
                placeMarker.setVisible(!placeMarker.isVisible());
            }
        } else if (nearby != null && nearby.containsKey(googLocation)) {
            Intent intent = new Intent(getActivity(), TempPlaceActivity.class);
            intent.putExtra("PLACE", nearby.get(googLocation));
            startActivity(intent);
            //   mCallbacks.onMarkerSelected("PLACE", nearby);
            return true;

代码有点含糊我很抱歉我不想post因为大约有 5 个不同 java.classes...只是一个正确方向的点极好的。 ResultsActivity.java 包含 2 个片段并有一个 results.xml。 MapFragment.java 包含地图,google 地点的详细信息在 frame.xml 中。 PlacesDetailsFragments 保存详细信息并保存在 places.xml 文件中。每个片段都有活动,它们到处都是,它们似乎是一对。我今晚放弃任何帮助将不胜感激!

我算出来了,想分享一下。好的,片段是这样写在 xml 布局中的...

<RelativeLayout
    android:id="@+id/search_frag"
    android:layout_width="match_parent"
    android:layout_height="@dimen/results"
    android:layout_below="@+id/top_banner"
    android:background="@color/cream"
    android:orientation="horizontal"
    tools:layout="@layout/result_hint"></RelativeLayout>

<RelativeLayout
    android:id="@+id/map_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/search_frag"
    android:background="@color/cream"
    android:orientation="horizontal"
    tools:layout="@layout/template_map_view" />

search_frag 和 map_fragment 需要是 RelativeLayout,因为要在 activity 中编程,您希望它们出现

ACTIVITY CLASS search_frag

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_results);
        setUpMapIfNeeded();

        ....
        //sets up default fragment, will later be replaced with fragment when marker is selected
        if (findViewById(R.id.fragment_container) == null) {

            if (savedInstanceState != null) {
                return;
            }

            FragmentManager fm = getSupportFragmentManager();
            SelectFrag firstFragment = new SelectFrag();
            FragmentTransaction ft = fm.beginTransaction();
                ft.add(R.id.search_frag, firstFragment);
                ft.commit();
            }
...

map_frag

private void setUpMapIfNeeded() {
        if (mMap != null) {
            return;
        }
       mMap = new MapFragment();
        mMap.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction()
                .add(R.id.map_fragment, mMap).commit();
    }

地图片段

  MarkerClickCB mCallbacks; //communicates with the activity
   //interface set up
     public interface MarkerClickCB {
            void onMarkerSelected(String place, GoogePlaces place1);
        }

    ....

 mMap.setOnMarkerClickListener(new OnMarkerClickListener() {

        @Override
        public boolean onMarkerClick(Marker marker) {

            if (!mClusterManager.onMarkerClick(marker)) {
                googLocation = marker;
                cluster.getPosition();
                if (googLocation.equals(userLocMarker) && clusts2 == null) {
                    searchFilter();
                }
                if (marker.equals(userLocMarker) && nearby != null) {
                    for (Marker placeMarker : nearby.keySet()) {
                        placeMarker.setVisible(!placeMarker.isVisible());
                    }
                } else if (cluster != null) {

                    mCallbacks.onMarkerSelected("PLACE", clickedClusterItem.getPlace());
                    getDistance(clickedClusterItem.getPosition().latitude, clickedClusterItem.getPosition().longitude);

                    return true;
                }
            }
            return false;
        }
    });

..现在回到 activity class 保存先前编程的片段。我们需要用包含所选标记的 google 位置信息的新片段替换 search_frag。

activityclass需要实现地图片段数据通信的回调数据

回到 ACTIVITY CLASS

public  class ResultsActivity extends AppCompatActivity
        implements MapFragment.MarkerClickCB
....
   //previous fragment info//  
...
     public void onMarkerSelected(String near, GoogePlaces googPlace) {

            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            PlaceFragment fragment = PlaceFragment.newInstance(googPlace);
            ft.replace(R.id.search_frag, fragment);
            ft.commit();
        }

..保存数据的替换片段 放置片段

    final static String PLACES_ID = "GoogePlaces";

    //Creates fragment object with details

    public static PlaceFragment newInstance(GoogePlaces place) {
            PlaceFragment fragment = new PlaceFragment();
            Bundle bundle = new Bundle();
            bundle.putSerializable(PLACES_ID, place);
            fragment.setArguments(bundle);

            return fragment;
        }

//this bit got a bit messy for me - welcome to comment with improvements 

   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        place = (GoogePlaces) getArguments().getSerializable(PLACES_ID);



 View view = inflater.inflate(R.layout.activity_place_detail, container, false);

    this.context = getActivity().getApplicationContext();

    Log.i("PLACE EXAMPLE", "Place Detail Activity");


        place = (GoogePlaces) getArguments().getSerializable(PLACES_ID);
...

        getActivity().setTitle(place.getName());
        String placesKey = getResources().getString(R.string.places_key);

//connect to google places - to gather eatery details
        String placesRequest = "https://maps.googleapis.com/maps/api/place/details/json?" +
                "key=" + placesKey + "&reference=" + place.getReference();
        PlacesDetailReadFeed detailTask = new PlacesDetailReadFeed();
        detailTask.execute(new String[]{placesRequest});

       ......

    return view;
    }

好吧,作为一个新手(ish)程序员,这对我来说非常困难我不得不承认,任何指针,对代码清理的批评将不胜感激