1

I am simply creating a Map Project and executing this code. But there is getMap() error. I have tried some example code from internet but there is also same error in getMap() function. 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.

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

Violet
  • 47
  • 12
  • What errors are you getting? Ideally including the full stack should help. – Acapulco Aug 03 '16 at 00:29
  • @Acapulco The Error Mesg showing is :- 12:59:34 AM IllegalStateException: Cannot locate factory for objects of type DefaultGradleConnector, as ConnectorServiceRegistry has been closed. 1:01:55 AM IllegalStateException: Cannot locate factory for objects of type DefaultGradleConnector, as ConnectorServiceRegistry has been closed. 1:02:47 AM IllegalStateException: Cannot locate factory for objects of type DefaultGradleConnector, as ConnectorServiceRegistry has been closed. – Violet Aug 03 '16 at 06:58
  • @Acapulco Can you please help me ? – Violet Aug 03 '16 at 15:23

1 Answers1

2

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

    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 .

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68