Google map not showing in android activity?

前端 未结 8 1034
生来不讨喜
生来不讨喜 2020-12-19 19:40

In my android app I want to have a MainActivity with a google map occupying the entire screen.

My problem is that the google map does not display a map, only the go

相关标签:
8条回答
  • 2020-12-19 19:53

    I just checked with google map like yours.

    Just compare your code with mine.

    MainActivity.java

    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    
    private GoogleMap mMap;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }
    
    
    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
    
        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
    

    }

    And Manifest

    <?xml version="1.0" encoding="utf-8"?>
    

    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality. 
    -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    
        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/. 
        -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />
    
        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    

    Note: You have to create a project in google

    click here to visit the google console

    Replace your Key value in the google_maps_api

    <resources>
    
    
    <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">Your Key</string>
    

    Hope this works for you.

    PS: you can generate google map activity from android studio.

    0 讨论(0)
  • 2020-12-19 20:03

    1) Please update google play services

    2) Please append your api key in the url

    Ex:-"https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters + "&key=" + MY_API_KEY;

    0 讨论(0)
  • 2020-12-19 20:07

    If the Activity is Opening and the map is not visible even if you are giving Google_map_key , then one solution that worked for me is that i created a new Google map API KEY by copying the link in the google_maps_api.xml and pasting in the browser. Select a project or create a project it will land you on the page where google API KEY is ready copy it and paste it in the place given in the instruction .it will work.

    0 讨论(0)
  • 2020-12-19 20:10

    Make sure that you have given these permisssions

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.INTERNET" />
    
    

    and check if the WiFi of AVD is on and your PC is connected to internet.

    I was getting the same problems, and following the above mentioned steps I was able to resolve my problem.

    0 讨论(0)
  • 2020-12-19 20:14

    Google maps likes to get the different activity lifecycle states. So you should send it: map_view.onCreate(savedInstanceState) and map_view.onResume(). It also likes onPause(), onLowMemory(), onSaveInstanceState(outState), onDestroy() if they occur.

    0 讨论(0)
  • 2020-12-19 20:15

    Include the following code in AndroidManifest.xml file, within the application part:

    <uses-library
        android:name="org.apache.http.legacy"
        android:required="false" />
    

    See documentation here.

    0 讨论(0)
提交回复
热议问题