gpt4 book ai didi

android - getMap() 函数出错

转载 作者:行者123 更新时间:2023-11-29 19:39:59 31 4
gpt4 key购买 nike

我只是创建一个 map 项目并执行此代码。但是有 getMap() 错误。我尝试了一些来自互联网的示例代码,但 getMap() 函数中也存在同样的错误。代码是:

    private GoogleMap mMap;
static final LatLng TutorialsPoint = new LatLng(21 , 57);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);


if (mMap == null) {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

try {
if (mMap == null) {

mMap = ((MapFragment) getFragmentManager().
findFragmentById(R.id.map)).getMap();
//findFragmentById(R.id.map)).getMap();
}
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
Marker TP = mMap.addMarker(new MarkerOptions().
position(TutorialsPoint).title("TutorialsPoint"));
}
catch (Exception e) {
e.printStackTrace();
}
}




@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

这是我的 Android list 文件。

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

<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

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

<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCBp-ide4ue3DKt-tud4SdzwzR652OSnGY" />
<!-- <meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />-->

<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

最佳答案

onMapReady 函数被触发时,您应该将标记添加到您的 map 中,这发生在您的 map 准备好使用时(例如添加标记等)。只需初始化您的 map 并调用 getMapAsync 功能类似

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

使用 googleMap 对象来操作您在 onMapReady 中提供给您的 map 。

关于android - getMap() 函数出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38726659/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com