簡體   English   中英

將Google Maps添加到Android App

[英]Adding Google Maps to an Android App

我和我的小組成員一直在嘗試在一項活動中添加Google地圖,但所有嘗試均失敗。 我們想尋求幫助。

我們遵循了這里的步驟。

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" />

我相信您必須擁有步驟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" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM