-2

我想制作谷歌地图以在 Android 中使用,但我有一些问题。

截屏

显现:

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

<uses-permission android:name="YOUR_PACKAGE_NAME.permission.MAPS_RECEIVE"
 android:protectionLevel="signature"/>
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="your.package.name.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission  android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


    <!-- Maps API needs OpenGL ES 2.0. -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <!-- You must insert your own Google Maps for Android API v2 key in here. -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCe29ye5iK83zx6rUBnCzulKCxf68nT-bs" />

        <activity
            android:name="com.example.odv.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>
    </application>

</manifest>

主要 XML:

    <?xml version="1.0" encoding="utf-8"?>
<!--
     Copyright (C) 2012 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<!-- This can go anywhere in your layout (see other demos for some examples). 
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.SupportMapFragment"/>
-->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

主要活动:

    package com.example.odv;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends android.support.v4.app.FragmentActivity  {

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

    @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;
    }

}

我想学安卓。我认为我有编程的算法智能,但我不知道如何开始学习编程。我没有很好的来源。

我写了这段代码,但我看不到地图。我只能看到白屏,无法打开地图。我能为此做些什么?

截屏

4

3 回答 3

0

这是我在测试应用程序中使用的地图的完整工作:

公共类 MainActivity 扩展 FragmentActivity {

private static final int GPS_ERRORDIALOG_REQUEST = 9001;
Button btn1;
EditText et1;
EditText et2;

GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (servicesOK()) {
        Toast.makeText(this, "Ready to map!!", Toast.LENGTH_LONG).show();
        setContentView(R.layout.testmap);

    } else {
        setContentView(R.layout.activitymain);

    }

    /*
     * et1 = (EditText)findViewById(R.id.edittext1); et2 =
     * (EditText)findViewById(R.id.edittext2); btn1 =
     * (Button)findViewById(R.id.button1);
     * 
     * btn1.setOnClickListener(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 void onClick(View v) {
 * 
 * Object lat = et1; Object lon = et2;
 * 
 * Uri uri=Uri.parse("geo:"+lat.toString()+","+lon.toString()); Intent i=new
 * Intent(Intent.ACTION_VIEW, uri); startActivity(i); Log.d("The Location:",
 * lat.toString()+lon.toString());
 * 
 * 
 * }
 */
public boolean servicesOK() {

    int isAvailable = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(this);

    if (isAvailable == ConnectionResult.SUCCESS) {

        return true;

    } else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {

        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,
                this, GPS_ERRORDIALOG_REQUEST);
        dialog.show();

    } else {

        Toast.makeText(this, "Cant connect!!", Toast.LENGTH_SHORT).show();

    }
    return false;
}

}

///////////////////////////////////////// ////////////////////////////////

这是布局:

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

这是Android清单:

package="com.mike.maps"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="18" />

<permission
    android:name="com.mike.maps.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.mike.maps.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

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


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.mike.maps.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>
    <activity android:name="com.mike.maps.SecondActivity" >
    </activity>

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="Your key goes here" >
    </meta-data>
</application>
于 2013-09-17T17:56:57.433 回答
0

您需要做的第一件事是,从API CONSOLE获取 API 密钥,然后您需要在 Android 清单中使用该 api 密钥: 1st 使用适当的权限:

二:使用功能

    android:glEsVersion="0x00020000"
    android:required="true" >

第三:在元数据中:

android:name="com.google.android.maps.v2.API_KEY" android:value="你的钥匙在这里" >

希望这可以帮助..:)

于 2013-09-17T17:22:40.947 回答
0

公共类 MainActivity 扩展 FragmentActivity {

private static final int GPS_ERRORDIALOG_REQUEST = 9001;
Button btn1;
EditText et1;
EditText et2;

GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (servicesOK()) {
        Toast.makeText(this, "Ready to map!!", Toast.LENGTH_LONG).show();
        setContentView(R.layout.testmap);

    } else {
        setContentView(R.layout.activitymain);

    }

    /*
     * et1 = (EditText)findViewById(R.id.edittext1); et2 =
     * (EditText)findViewById(R.id.edittext2); btn1 =
     * (Button)findViewById(R.id.button1);
     * 
     * btn1.setOnClickListener(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 void onClick(View v) {
 * 
 * Object lat = et1; Object lon = et2;
 * 
 * Uri uri=Uri.parse("geo:"+lat.toString()+","+lon.toString()); Intent i=new
 * Intent(Intent.ACTION_VIEW, uri); startActivity(i); Log.d("The Location:",
 * lat.toString()+lon.toString());
 * 
 * 
 * }
 */
public boolean servicesOK() {

    int isAvailable = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(this);

    if (isAvailable == ConnectionResult.SUCCESS) {

        return true;

    } else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {

        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,
                this, GPS_ERRORDIALOG_REQUEST);
        dialog.show();

    } else {

        Toast.makeText(this, "Cant connect!!", Toast.LENGTH_SHORT).show();

    }
    return false;
}

}

另外,我只是布局中的一个地图视图,请不要忘记将 API KEY 放在清单中,因为没有它,地图将无法加载。并且要使用地图,请使用真实设备而不是模拟器..:)

于 2013-09-17T17:27:31.653 回答