【问题标题】:How can I get my current location ? [Android Google Map V2]如何获取当前位置? 【安卓谷歌地图V2】
【发布时间】:2014-03-19 10:29:53
【问题描述】:

这是我的 .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>

正如你们所说的,我有

-将Activity改为FragmentActivity

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

【问题讨论】:

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


    【解决方案1】:

    你需要改变

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

    因为在您的 xml 字段中您使用了 SupportMapFragment 类。

    另一件事是将您的类扩展为 FragmentActivity 而不是 Activity

    删除这些

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

    来自您的清单文件。

    【讨论】:

      【解决方案2】:

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

      尝试如下加载地图:

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

      【讨论】:

      • 有 2 个错误是: - 方法 getSupportFragmentManager() 未定义 GoogleMap 类型和 - 类型不匹配:无法从 com.google.android.gms.maps.GoogleMap 转换为 com.example .locdroid_v2.GoogleMap
      • @user3424059 您必须在项目的构建路径中添加片段支持库android-support-v4.jar 和谷歌地图库,然后才能获得它。
      • pelu open gls ma seekbar wadu...online tha
      • 我按照你说的做了改变,我已经添加了 android-support-v4.jar 但我仍然有这个错误:“类型不匹配:无法从 com.google.android.gms.maps.GoogleMap 转换为com.example.locdroid_v2.GoogleMap"
      • 将您的活动更改为FragmentActivity。 @user3424059
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      相关资源
      最近更新 更多