简体   繁体   English

将Google Maps添加到Android App

[英]Adding Google Maps to an Android App

Me and my group members have been trying to add Google Maps in one of our activities but all attempts failed. 我和我的小组成员一直在尝试在一项活动中添加Google地图,但所有尝试均失败。 We'd like to ask for some help. 我们想寻求帮助。

We followed the steps here . 我们遵循了这里的步骤。

AndroidManifest.xml 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 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 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" />

I believe you have to have the android key which is described in step 4. and replace 我相信您必须拥有步骤4中所述的android密钥,然后替换

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

with

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

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

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