How is it right to connect Google Map to Android project?



  • I have a question for Google Map to Android project.
    In general, I want to see a map in my fragment, but alas, I can't understand what I did wrong. Engaged on instructions from the official documents to API.

    Here are my leafing code:

    Manifest:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hfad.mapsstudy">
    

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

    <uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:theme="@style/AppTheme.NoActionBar">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />

            &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;
    
    &lt;meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" /&gt;
    &lt;meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIzaSyC****************************eKI"/&gt;
    

    </application>

    build.gradle:

    apply plugin: 'com.android.application'

    android {
    compileSdkVersion 24
    buildToolsVersion "23.0.2"

    defaultConfig {
    applicationId "com.hfad.mapsstudy"
    minSdkVersion 10
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
    }
    buildTypes {
    release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    }
    }

    dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.android.support:design:24.0.0'
    compile 'com.google.android.gms:play-services:9.0.2'
    }

    content_main:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.hfad.mapsstudy.MainActivity"
    tools:showIn="@layout/activity_main">

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
    </RelativeLayout>

    MainActivity:

    package com.hfad.mapsstudy;

    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v4.app.FragmentActivity;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.view.Menu;
    import android.view.MenuItem;

    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.MapFragment;
    import com.google.android.gms.maps.OnMapReadyCallback;
    import com.google.android.gms.maps.SupportMapFragment;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.MarkerOptions;

    public class MainActivity extends FragmentActivity implements OnMapReadyCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    
    mapFragment.getMapAsync(this);
    
    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);
    

    }

    @Override
    public void onMapReady(GoogleMap googleMap)
    {
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    googleMap.addMarker(new MarkerOptions()
            .position(new LatLng(0, 0))
            .title("Marker"));
    

    }
    }

    Gradle Console makes a mistake:

    FAILURE: Build failed with an exception.

    • What went wrong:
      Execution failed for task ':app:transformClassesWithDexForDebug'.
      > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_72\bin\java.exe'' finished with non-zero exit value 2

    • Try:
      Run with --stacktrace option to get the stack trace. Run with --info or -- debug option to get more log output.



  • When you're connected to play-service, you're almost assured of 64cc+ methods in your annex. To collect such a project, multiDex should be added

    https://developer.android.com/studio/build/multidex.html

    And in order to avoid such a problem, don't connect the whole package play-service and use it.

    com.google.android.gms:play-services-maps:9.0.2
    

    I'll also give a google page reference to the libraries that connect.

    https://developers.google.com/android/guides/setup#add_google_play_services_to_your_project




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2