简体   繁体   English

在标签片段android中添加谷歌地方选择器

[英]Adding google place picker in tab fragment android

I'm currently developing application about google maps services. 我目前正在开发有关谷歌地图服务的应用程序。 I use tab layout with 2 tabs which are fragments. 我使用带有2个标签的标签布局。 In one tab I use google maps api and it work's properly. 在一个标签中,我使用谷歌地图api,它工作正常。 But in second tab I want that on button click start intent with place picker. 但是在第二个标签中我希望按钮点击开始意图与地方选择器。 On click it appears for 1 second and hide after that. 点击它会显示1秒钟,然后隐藏。 I don't know what's the problem, so please help. 我不知道有什么问题,所以请帮忙。

Here's the code: 这是代码:

MainActivity: 主要活动:

public class MainActivity extends AppCompatActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    private SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

//        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//        fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();
//            }
//        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    TabDirectionsActivity tabDirectionsActivity = new TabDirectionsActivity();
                    return tabDirectionsActivity;
                case 1:
                    TabPlacesActivity tabPlacesActivity = new TabPlacesActivity();
                    return tabPlacesActivity;
                default:
                    return  null;
            }
        }

        @Override
        public int getCount() {
            // Show 2 total pages.
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "DIRECTIONS";
                case 1:
                    return "PLACES";
            }
            return null;
        }
    }
}

Here's the TabPlaces Fragment: 这是TabPlaces片段:

public class TabPlacesActivity extends Fragment {

    TextView placeNameText;
    TextView placeAddressText;
    WebView attributionText;
    Button getPlaceButton;
    private final static int PLACE_PICKER_REQUEST = 1;

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

        View placesView = inflater.inflate(R.layout.activity_places, container, false);

        placeNameText = (TextView) placesView.findViewById(R.id.tv_PlaceName);
        placeAddressText = (TextView) placesView.findViewById(R.id.tv_PlaceAddress);
        attributionText = (WebView) placesView.findViewById(R.id.wv_Attribution);
        getPlaceButton = (Button) placesView.findViewById(R.id.btn_GetPlace);

        getPlaceButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
                try {
                    Intent intent = builder.build(getActivity());
                    startActivityForResult(intent, PLACE_PICKER_REQUEST);
                } catch (GooglePlayServicesRepairableException e) {
                    e.printStackTrace();
                } catch (GooglePlayServicesNotAvailableException e) {
                    e.printStackTrace();
                }

            }
        });

        return placesView;

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PLACE_PICKER_REQUEST){
            if (resultCode == getActivity().RESULT_OK){
                Place place = PlacePicker.getPlace(getActivity(),data);
                placeNameText.setText(place.getName());
                placeAddressText.setText(place.getAddress());
                if (place.getAttributions() == null) {
                    attributionText.loadData("no attribution", "text/html; charset=utf-8", "UFT-8");
                } else {
                    attributionText.loadData(place.getAttributions().toString(), "text/html; charset=utf-8", "UFT-8");
                }
            }
        }
    }
}

Here's the activity_places.xml: 这是activity_places.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="rs.fon.mapapp.TabPlacesActivity"
    android:gravity="top|center">

    <Button
        android:id="@+id/btn_GetPlace"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get Place" />

    <TextView
        android:id="@+id/tv_PlaceName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:text="Place Name"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv_PlaceAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Place Address"
        android:textSize="16sp" />

    <WebView
        android:id="@+id/wv_Attribution"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Attribution"
        android:textSize="16sp" />


</LinearLayout>

I found the solution. 我找到了解决方案。 The solution was that I have to rename my meta-data in Manifest file 解决方案是我必须在Manifest文件中重命名我的元数据

<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="YOUR API KEY" />

and change to 并改为

<meta-data android:name="com.google.android.geo.API_KEY"
            android:value="YOUR ANOTHER API KEY"
            />

and also remove signature 并删除签名

<permission
        android:name="rs.fon.mapapp.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="rs.fon.mapapp.permission.MAPS_RECEIVE" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM