如何在Android设备中获取地图

时间:2014-11-26 07:46:50

标签: android google-maps

我在很多网站上搜索并用Google搜索显示地图。我是android开发的新手。

这是我的清单文件。

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <permission
        android:name="com.example.googlemap.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.googlemap.permission.MAPS_RECEIVE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <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-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

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

        <!-- Goolge Maps API Key -->
        <meta-data
             android:name="com.google.android.maps.v2.API_KEY"
             android:value="my_google_api_key" />

        <activity
            android:name="com.example.googlemap.MapActivity"
            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>

- &GT;这是我的主要活动,我已经编写了这段代码来显示地图当前位置。

int status=GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

//Showing status
if(status!=ConnectionResult.SUCCESS){//Google playservices are not available

    int requestCode=10;
    Dialog d=GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
    d.show();
}
else{//Google playservices are available

    //Getting referance to SupportMap Fragment of activity_map.xml
    SupportMapFragment fm=(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

    //Getting googlemap object from fragment
    googlemap=fm.getMap();

    //Enable Mylocation of google map
    googlemap.setMyLocationEnabled(true);

    //Getting locatinmanager object from System services LOCATION_SERVICE
    LocationManager locationmanager=(LocationManager) getSystemService(LOCATION_SERVICE);

    //Creating criteria object to retrive provider
    Criteria criteria=new Criteria();

    //Getting name of the best provider
    String provider=locationmanager.getBestProvider(criteria, true);

    //Getting currunt location
    Location location=locationmanager.getLastKnownLocation(provider);   

    locationmanager.requestLocationUpdates(provider, 20000, 0, this);
    if(location!=null){

        onLocationChanged(location);
    }
}

提前致谢。

4 个答案:

答案 0 :(得分:0)

您在Manifest的application代码中错过了Google Play服务版本标记:

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

答案 1 :(得分:0)

这是您可以从中获得所需答案的链接。我相信它会帮助你 GoogleMap

答案 2 :(得分:0)

您的宣言文件应如下所示:

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

    <permission
        android:name="com.errorpoint.findlocation.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.errorpoint.findlocation.permission.MAPS_RECEIVE" />

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="21" />

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

    <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" >
        <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.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="Key goes here" />
    </application>

</manifest>

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

如需更多下载git app:

答案 3 :(得分:0)

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

请不要上面的“你的钥匙”。你必须在那里添加你的密钥。