简体   繁体   English

getMap()函数错误

[英]Error in getMap() Function

I am simply creating a Map Project and executing this code. 我只是创建一个Map Project并执行此代码。 But there is getMap() error. 但是有getMap()错误。 I have tried some example code from internet but there is also same error in getMap() function. 我已经尝试了一些来自互联网的示例代码,但是getMap()函数中也存在相同的错误。 The Code is : 代码是:

    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));
}

This is my Android Manifest File. 这是我的Android清单文件。

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

you should add marker into your map when onMapReady function is triggered and this happen when your map is ready to use (like adding marker etc).just initialize your map and call getMapAsync function like 您应该在触发onMapReady函数时将标记添加到地图中,并且这会在您的地图准备好使用时发生(例如添加标记等)。只需初始化地图并调用getMapAsync函数即可

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

use googleMap object to manipulate you map which is given to you inside onMapReady . 使用googleMap对象来操作您在onMapReady提供给您的onMapReady

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM