我怎样才能获得当前位置? [Android Google Map V2]

时间:2014-03-19 10:29:54

标签: android google-maps google-maps-markers google-maps-api-2

这是我的.Java文件

import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import android.app.Activity;
import android.os.Bundle;

public class GoogleMap extends Activity {

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

        // Get a handle to the Map Fragment
        GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        LatLng sydney = new LatLng(-33.867, 151.206);

        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

        map.addMarker(new MarkerOptions().title("Sydney").snippet("The most populous city in Australia.").position(sydney));
    }
}

XML

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



//GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

有一个错误:

Type mismatch: cannot convert from com.google.android.gms.maps.GoogleMap to com.example.locdroid_v2.GoogleMap

清单文件

我的宣言:

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

    <uses-sdk 
        android:minSdkVersion="12"
        android:targetSdkVersion="19" />
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>   

    <permission android:name="com.example.locdroid_v2.MAPS_RECEIVE" android:protectionLevel="signature"/>
    <uses-permission android:name="com.example.locdroid_v2.MAPS_RECEIVE" />
    <uses-permission android:name="com.example.locdroid_v2.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.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" />



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

        <uses-library android:name="com.google.android.maps" />



        <activity
            android:name="com.example.locdroid_v2.MainActivity"
            android:label="@string/app_name" 
            android:screenOrientation="portrait" >

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

        </activity>



        <activity android:name="com.example.locdroid_v2.GoogleMap"
                  android:screenOrientation="portrait" >

        </activity>

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

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

       </application>

</manifest>

正如你们所说的那样

- 将活动改为FragmentActivity

-GoogleMap map =((SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.map))。getMap();

2 个答案:

答案 0 :(得分:2)

由于您已在布局文件中为SupportMapFragment声明了一个类,因此必须使用类SupportMapFragment在您的java文件中转换GoogleMap

尝试加载地图,如下所示:

 GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

答案 1 :(得分:2)

您需要更改

 GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

因为在您的xml领域中您使用了 SupportMapFragment 类。

另一件事是将您的课程扩展为 FragmentActivity 而不是活动

删除这些

 <uses-library android:name="com.google.android.maps" />
 <activity android:name="com.example.locdroid_v2.GoogleMap"
           android:screenOrientation="portrait" >

或者将您的活动名称从GoogleMap更改为另一个并注册为

 <activity android:name="com.example.locdroid_v2.MyGoogleMapActivity"
           android:screenOrientation="portrait" >

来自您的清单文件。