I have looked through about 10 stackoverflow pages on this topic and none of the solutions are helping me. I have copied the code from the first answer in this post, but I am still getting a lot of errors. What I want is to create the map in a class called MapFragment
, and then display it in a tab of an app.
As of now here is my code: Java:
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>
Manifest:
<?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>
my dependencies:
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'
}
I do not understand what is going on. Is there some import I am missing class I need to cut and paste to make this work? On a diferent tutorial they had me import this
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
but on both lines I get an error cannot resolve symbol 'android'
.
I am currently using android studio. The API key is entered in my actual compute I just left it out from the stack overflow post.