【发布时间】: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