【问题标题】:Having trouble creating google maps in a fragment在片段中创建谷歌地图时遇到问题
【发布时间】:2015-07-02 15:43:15
【问题描述】:

我浏览了大约 10 个有关此主题的 stackoverflow 页面,但没有一个解决方案对我有帮助。我已经从this post 的第一个答案中复制了代码,但我仍然遇到很多错误。我想要的是在一个名为MapFragment 的类中创建地图,然后将其显示在应用程序的选项卡中。

到目前为止,这是我的代码: 爪哇:

public class MapFragment extends Fragment {
    private static final String ARG_SECTION_NUMBER = "section_number";
    private final LatLng HAMBURG = new LatLng(53.558, 9.927);   //Error: cannot resolve symbol 'LatLng'
    private final LatLng KIEL = new LatLng(53.551, 9.993);      //Error: cannot resolve symbol'LatLng'
    private GoogleMap map;                    //Error: cannot resolve symbol GoogleMap

    public MapFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View rootView = inflater.inflate(R.layout.fragment_map, container, false);

        map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();      //Error:cannot resolve symbol 'SupportMapFragment'

        Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
                .title("Hamburg"));                          //Error:cannot resolve symbol 'Marker' or 'MarkerOptions' or method 'addMarker'
        Marker kiel = map.addMarker(new MarkerOptions()      //Error:cannot resolve symbol 'Marker' or 'MarkerOptions' or method 'addMarker'
                .position(KIEL)
                .title("Kiel")
                .snippet("Kiel is cool")
                .icon(BitmapDescriptorFactory
                .fromResource(R.drawable.ic_launcher)));

        // Move the camera instantly to hamburg with a zoom of 15.
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));    //Error:cannot resolve symbol 'CameraUpdateFactory' or method 'moveCamera'

        // Zoom in, animating the camera.
        map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);//Error:cannot resolve symbol 'CameraUpdateFactory' or method 'animateCamera'

        //...

        return rootView;
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity" >

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

</RelativeLayout>

清单:

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

    <permission
        android:name="com.example.lastgmap.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

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

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/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="MY_API_KEY" />

    </application>

</manifest>

我的依赖:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.google.android.gms:play-services:7.5.0'
}

我不明白发生了什么。是否有一些导入我缺少我需要剪切和粘贴才能完成这项工作的课程?在另一个教程中,他们让我导入了这个

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

但在这两行中我都收到错误cannot resolve symbol 'android'

我目前正在使用安卓工作室。 API 密钥是在我的实际计算中输入的,我只是从堆栈溢出帖子中忽略了它。

【问题讨论】:

    标签: java android google-maps android-fragments google-maps-android-api-2


    【解决方案1】:

    使用 Google 地图时请注意以下步骤。

    确保您已添加 Google Play Services Library作为Library项目。

    如果您确定上述步骤但问题仍未解决 请通过 SDK 管理器更新您的 Google Play 服务库

    如果您已更新库但问题仍未解决,请执行 CleanReBuild 项目。

    如果您执行了上述步骤,但问题仍未解决,请重新启动您的 Eclipse,有时它会神奇地起作用。

    希望对您有所帮助。

    【讨论】:

    • 你知道我将如何在 android studio 中添加 Google Play 服务库吗?
    • @AlecS,您必须右键单击项目属性-> 然后选择第二个 Android 选项卡。请参考stackoverflow.com/questions/26457096/… 和右侧您可以检查添加选项。选择它,添加你的库项目->点击应用->点击确定。
    • This 是我所看到的。项目属性选项卡在哪里?
    • @AlecS,如果您使用的是 Eclipse,请右键单击项目,然后选择最后一个选项属性
    • @AlecS,如果您使用的是 android studio 然后单击文件-> 项目结构-> 然后转到最后一个选项-> 依赖项->单击 + 按钮-> 然后选择库依赖项-> 搜索谷歌播放服务库。
    【解决方案2】:

    您需要在问题中添加更多细节。 - 你使用 Android Studio 还是 Eclipse? - 您是否将您的谷歌地图密钥添加到您的清单中。 - 如果使用 Android Studio,您是否将 google play 服务的依赖项添加到您的 gradle 构建文件中?可能是因为你忘了添加依赖,所以编译器找不到你的类

    【讨论】:

    • 我目前正在使用android studio。 API 密钥是在我的实际计算中输入的,我只是从堆栈溢出帖子中忽略了它。我已经在上面发布了我的依赖项。
    • 您能否详细说明您的错误?上面,你只是在导入时给了我两个错误。
    猜你喜欢
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多