将Google地图添加到Android应用

时间:2016-08-19 20:09:51

标签: java android eclipse google-maps

我和我的小组成员一直在尝试在我们的某个活动中添加Google地图,但所有尝试都失败了。我们想请一些帮助。

我们按照here步骤进行了操作。

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exapp.maptrial01"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="17"
    android:targetSdkVersion="22" />

<permission
    android:name = "com.exapp.maptrial01.permission.MAPS_RECEIVE"
    android:protectionLevel = "signature" />

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

<uses-permission android:name = "com.exapp.maptrial01.permission.MAPS_RECEIVE" />
<uses-permission android:name = "android.permission.INTERNET" />
<uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name = "android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name = "android.permission.ACCESS_FINE_LOCATION" />

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

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

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
</application>

</manifest>

MainActivity.java

package com.exapp.maptrial01;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
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 {

static final LatLng MarqueeMall = new LatLng(15.163350, 120.609495);
private GoogleMap gMap;

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

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}   

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onMapReady(GoogleMap arg0) {
    // TODO Auto-generated method stub
    gMap = arg0;

    // Add a marker in Sydney, Australia, and move the camera.
    LatLng sydney = new LatLng(15.163350, 120.609495);
    gMap.addMarker(new MarkerOptions().position(sydney).title("Marker in MQ"));
    gMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
  }
}

activity_main.xml中

<fragment 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:id="@+id/map"
tools:context=".MainActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />

1 个答案:

答案 0 :(得分:0)

我相信你必须拥有步骤4中描述的android键并替换

    android:name="com.google.android.maps.v2.API_KEY"
    android:value="intentionally left blank" />

    android:name="com.google.android.maps.v2.API_KEY"
    android:value="xxxxxxxxxxxxxxxxx" />