Failed to load map. Error contacting Google server

2019-01-02 20:56发布

I search all days and I can't find the answer...

everything is ready but google map still can't show on fragment with tab

I check that I already turn on google map android v2 and right API_KEY

I have no idea what happened!

img

MainActivity.java:

public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;

    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String titles[] = { this.getString(R.string.title_around),
                this.getString(R.string.title_map),
                this.getString(R.string.title_favorite),
                this.getString(R.string.title_achievement), };

        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager
                .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                    }
                });

        for (int i = 0; i < titles.length; i++) {
            actionBar.addTab(actionBar.newTab().setText(titles[i])
                    .setTabListener(this));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            // TODO 在這邊決定哪個位置要給什麼 View(Fragment)
            Fragment fragment = null;
            // TODO 建議先在之前就初始化好 4 個 Fragment
            switch (position) {
            case 0:
                fragment = new AroundFragment();
                break;
            case 1:
                fragment = new MapsFragment();
                break;
            case 2:
                fragment = new FavoriteFragment();
                break;
            case 3:
                fragment = new AchievementFragment();
                break;
            default:
                break;
            }

            return fragment;
        }

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            // TODO 在這邊決定每一個 View 的 title
            switch (position) {
            case 0:
                return getString(R.string.title_around);
            case 1:
                return getString(R.string.title_map);
            case 2:
                return getString(R.string.title_favorite);
            case 3:
                return getString(R.string.title_achievement);
            }
            return null;
        }
    }

}

MapsFragment.java:

public class MapsFragment extends Fragment{
    static final LatLng NKUT = new LatLng(23.979548, 120.696745);
    private GoogleMap map;
    SupportMapFragment mMapFragment;
    public MapsFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.map_fragment,container, false);

        map = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

         mMapFragment = SupportMapFragment.newInstance();
         FragmentTransaction fragmentTransaction =
                 getFragmentManager().beginTransaction();
         fragmentTransaction.add(R.id.map,mMapFragment);
         fragmentTransaction.commit();

        return rootView;

    }
}

Map_fragment.xml

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout> 

Manifest.xml:

<!-- Google Map -->
<permission
        android:name="com.jertt.yummymap.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
<uses-permission android:name="com.jertt.yummymap.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<!-- end -->
    <!-- Google Map API Key -->

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

    <!-- end -->

Solution:

I found the solution!!!

If you use private keystore to apply API_KEY. You cannot upload and install application with eclipse.

You need to upload .apk file to your device by yourself and install it!

Thanks Tarsem and srikanth gr help!!

25条回答
笑指拈花
2楼-- · 2019-01-02 21:22

I was able to use the debug key and load from eclipse. I was missing the following permission in my AndroidManifest.xml

uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"
查看更多
只若初见
3楼-- · 2019-01-02 21:23

I have fixed the issue by giving the permission in the manifest

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
查看更多
人间绝色
4楼-- · 2019-01-02 21:23

In my case what worked is that I had to install it via command line :

adb install /path/to/app/MyApp.apk

查看更多
裙下三千臣
5楼-- · 2019-01-02 21:24

I was facing the same problem. I have tried several solutions - none worked for me. What worked for me was generating a new API key and CLEARING the DATA of the Google play service on my device.

查看更多
有味是清欢
6楼-- · 2019-01-02 21:25

After reviewing a lot of cases and successfully run the maps, these things are important to have in mind:

  • Use android api 17, booth emulator and project
  • Reference the google services
  • Use the android support if needed
  • Use the following order in libraries: src, gen, google apis, android support, dependencies and private libraries
  • Use a correct api key
  • Clear data, re-install app.
查看更多
无色无味的生活
7楼-- · 2019-01-02 21:27

All you have to do is: use the debug key in your code; uninstall the app; install the new app with the debug key.

查看更多
登录 后发表回答