Question

Here is my .Java file

import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import android.app.Activity;
import android.os.Bundle;

public class GoogleMap extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.googlemap);

        // Get a handle to the Map Fragment
        GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        LatLng sydney = new LatLng(-33.867, 151.206);

        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

        map.addMarker(new MarkerOptions().title("Sydney").snippet("The most populous city in Australia.").position(sydney));
    }
}

XML

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



//GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

There is an error which is :

Type mismatch: cannot convert from com.google.android.gms.maps.GoogleMap to com.example.locdroid_v2.GoogleMap

Manifest File

My Manifest :

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

    <uses-sdk 
        android:minSdkVersion="12"
        android:targetSdkVersion="19" />
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>   

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



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

        <uses-library android:name="com.google.android.maps" />



        <activity
            android:name="com.example.locdroid_v2.MainActivity"
            android:label="@string/app_name" 
            android:screenOrientation="portrait" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>



        <activity android:name="com.example.locdroid_v2.GoogleMap"
                  android:screenOrientation="portrait" >

        </activity>

        <meta-data 
             android:name="com.google.android.maps.v2.API_KEY" 
             android:value="MyKey"/>

         <meta-data
            android:name="com.google.android.gms.version"
            android:value="@+integer/google_play_services_version" />

       </application>

</manifest>

As you all said i had

-Change Activity to FragmentActivity

-GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

Was it helpful?

Solution

You need to change

 GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

because in your xml fiel you have used SupportMapFragment class.

Another thing is that extends your class as a FragmentActivity instead of Activity.

Remove these

 <uses-library android:name="com.google.android.maps" />
 <activity android:name="com.example.locdroid_v2.GoogleMap"
           android:screenOrientation="portrait" >

Or change your Activity Name from GoogleMap to another and register as

 <activity android:name="com.example.locdroid_v2.MyGoogleMapActivity"
           android:screenOrientation="portrait" >

from your manifest file.

OTHER TIPS

As you have declared a class for the SupportMapFragment in your layout file so you have to use the class SupportMapFragment to cast your GoogleMap in your java file.

Try to load the map as below:

 GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top