"Error inflating class fragment" with google map on xamarin - google-maps

I have xamarin for an android app. I would like an ActionBar with tabs and a google map on one tab.
It works but only one time when i click on the map tab.
I explain, the app launched, im on the event tab, i click on map tab, the map works correctly but when i go back to event tab and again map tab, it crash.
Main:
ActionBar.SetDisplayShowHomeEnabled (true);
ActionBar.SetDisplayShowHomeEnabled (true);
ActionBar.SetDisplayShowTitleEnabled (true);
ActionBar.SetTitle (Resource.String.app_name);
ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
SetContentView (Resource.Layout.Main);
var fragMap = new MapPageFragment();
ActionBar.Tab tab2 = ActionBar.NewTab();
tab2 = ActionBar.NewTab();
tab2.SetText("Map");
tab2.TabSelected += delegate(object sender, ActionBar.TabEventArgs e)
{
e.FragmentTransaction.Replace(Resource.Id.fragment_container, fragMap);
};
ActionBar.AddTab(tab2);
MapPageFragment:
namespace OnmappTest
{
public class MapPageFragment : Android.App.Fragment
{
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
view = inflater.Inflate(Resource.Layout.MapPage, container, false);
The error
binary xml file line #1: error inflating class fragment" on "view =
inflater.Inflate(Resource.Layout.MapPage, container, false);
MapPage
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="350dp"
class="com.google.android.gms.maps.MapFragment" />
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="OnmappTest.OnmappTest">
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="21" />
<!-- Google Maps for Android v2 requires OpenGL ES v2 -->
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<!-- We need to be able to download map tiles and access Google Play Services-->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Allow the application to access Google web-based services. -->
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- Google Maps for Android v2 will cache map tiles on external storage -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Google Maps for Android v2 needs this permission so that it may check the connection state as it must download data -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application android:label="OnmappTest" android:icon="#drawable/icon">
<!-- Put your Google Maps V2 API Key here. -->
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="value" />
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
</application>
I tried SupportMapFragment instead of MapFragment, FragmentActivity instead of Fragment but nothing

I had the same problem. Please add this to OnPause method:
protected override void OnPause()
{
base.OnPause();
if (_mapFragment != null)
{
FragmentManager fragmentManager = FragmentManager;
fragmentManager.BeginTransaction().Remove(_mapFragment).Commit();
}
}
This solved my problem, hope that will solve yours.

Related

How to implement Google maps in Xamarin Forms (Android and iOS)? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have a Google map API key and I want to use that to show maps in my Android app and iOS app using Xamarin Forms. Which library would you use to have less conflict on both OS?
An easy way to implement that is using the NuGet Xamarin.Forms.GoogleMaps
Xamarin.Forms.GoogleMaps features:
Map types
Traffic map
Map events
Panning with animation
Panning directly
Pins
Custom Pins
Pin drag & drop
Polygons
Lines
Circles
Custom map tiles
Follow the next steps to set up maps in your project:
Install the NuGet package Xamarin.Forms.GoogleMaps in all projects.
Android. Initialize the library in your MainActivity.cs in the OnCreate method:
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
Xamarin.FormsGoogleMaps.Init(this, savedInstanceState); //Initialize GoogleMaps here
LoadApplication(new App());
}
In your AndroidManifest.xml.
Add the properties com.google.android.geo.API_KEY com.google.android.gms.version org.apache.http.legacy inside the tag <application>.
Also adds the required permissions ACCESS_COARSE_LOCATION ACCESS_FINE_LOCATION.
Add some uses-feature if you will use geolocation.
Your AndroidManifest.xml should look like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="yvan.eht.nioj" android:installLocation="auto">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
<application android:label="YourApp.Android">
<meta-data android:name="com.google.android.geo.API_KEY" android:value="Your_Api_Key_Here" />
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
<uses-library android:name="org.apache.http.legacy" android:required="false" />
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.location" android:required="false" />
<uses-feature android:name="android.hardware.location.gps" android:required="false" />
<uses-feature android:name="android.hardware.location.network" android:required="false" />
</manifest>
iOS. Initialize the library in your AppDelegate.cs in the FinishedLaunching method:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
Xamarin.FormsGoogleMaps.Init("Your_Api_Key_Here");
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
In your Info.plist add the properties NSLocationAlwaysUsageDescription NSLocationWhenInUseUsageDescription NSLocationAlwaysAndWhenInUseUsageDescription
<? xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!--Your other Permissions may be on top -->
<!-- Just add the Permissions below -->
<key>NSLocationAlwaysUsageDescription</key>
<string>Can we use your location at all times?</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Can we use your location when your application is being used?</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Can we use your location at all times?</string>
</dict>
</plist>
DONE
Now you can add a map in your xaml and show it in your Android and iOS app like this:
<?xml version="1.0" encoding="utf-8"?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:maps="clr-namespace:Xamarin.Forms.GoogleMaps;assembly=Xamarin.Forms.GoogleMaps"
mc:Ignorable="d"
x:Class="YourApp.MainPage">
<ContentPage.Content>
<Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<maps:Map x:Name="map" VerticalOptions="FillAndExpand"></maps:Map>
</Grid>
</ContentPage.Content>
</ContentPage>
OPTIONAL
Request runtime location permissions
If your application targets API 23 or later and needs to access the user's location, it must check to see if it has the required permission at runtime, and request it if it does not have it. This can be accomplished as follows:
In the MainActivity class, add the following fields:
const int RequestLocationId = 0;
readonly string[] LocationPermissions =
{
Manifest.Permission.AccessCoarseLocation,
Manifest.Permission.AccessFineLocation
};
In the MainActivity class, add the following OnStart override:
protected override void OnStart()
{
base.OnStart();
if ((int)Build.VERSION.SdkInt >= 23)
{
if (CheckSelfPermission(Manifest.Permission.AccessFineLocation) != Permission.Granted)
{
RequestPermissions(LocationPermissions, RequestLocationId);
}
else
{
// Permissions already granted - display a message.
}
}
}
(Not necessary if you are using Xamarin Essentials) In the MainActivity class, add the following OnRequestPermissionsResult override:
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
if (requestCode == RequestLocationId)
{
if ((grantResults.Length == 1) && (grantResults[0] == (int)Permission.Granted))
// Permissions granted - display a message.
else
// Permissions denied - display a message.
}
else
{
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}

Google maps not created, callback function never runs

I am trying to add google maps support to my android app. As I create whole application layout in code, I would like to create also the map object in code. This is class that should handle OnMapReady callback function.
public class MapReader : Java.Lang.Object, IOnMapReadyCallback
{
private LayoutBuilder layoutBuilder;
public MapReader(LayoutBuilder lb)
{
layoutBuilder = lb;
}
public void OnMapReady(GoogleMap googleMap)
{
layoutBuilder.OnMapLoaded(googleMap);
}
}
This is critical part of code creating the layout.
MapView mapView=new MapView(context);
mapView.LayoutParameters = layoutParams;
linearLayout.AddView(mapView);
mapReader = new MapReader(this);
mapView.GetMapAsync(mapReader);
The problem is that I get no error/exception. The OnMapReady function is never called and the screen is just empty. What can be wrong or how can I get additional info about such a behavior? I tried to follow steps from this video. In output window I tried to find lines that could help:
Loaded assembly: /storage/emulated/0/Android/data/AlienChessAndroid.AlienChessAndroid/files/.__override__/Xamarin.GooglePlayServices.Base.dll [External]
Loaded assembly: /storage/emulated/0/Android/data/AlienChessAndroid.AlienChessAndroid/files/.__override__/Xamarin.GooglePlayServices.Basement.dll [External]
Loaded assembly: /storage/emulated/0/Android/data/AlienChessAndroid.AlienChessAndroid/files/.__override__/Xamarin.GooglePlayServices.Maps.dll [External]
There are a few lines in output window that don't look good but the application runs.
AOT module 'Mono.Android.dll.so' not found: dlopen failed: library "/data/app/AlienChessAndroid.AlienChessAndroid-1/lib/arm/libaot-Mono.Android.dll.so" not found
AOT module '/storage/emulated/0/Android/data/AlienChessAndroid.AlienChessAndroid/files/.__override__/Xamarin.Android.Support.v4.dll.so' not found: dlopen failed: library "/data/app/AlienChessAndroid.AlienChessAndroid-1/lib/arm/libaot-Xamarin.Android.Support.v4.dll.so" not found
AOT module '/Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/builds/install/mono-armv7/lib/mono/aot-cache/arm/Xamarin.Android.Support.v4.dll.so' not found: dlopen failed: library "/data/app/AlienChessAndroid.AlienChessAndroid-1/lib/arm/libaot-Xamarin.Android.Support.v4.dll.so" not found
This is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="AlienChessAndroid.AlienChessAndroid" android:versionCode="1" android:versionName="1.0" android:installLocation="preferExternal">
<uses-sdk android:minSdkVersion="22" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application android:label="Alien Chess" android:icon="#drawable/Alien" android:largeHeap="true">
<meta-data android:name="com.google.android.gms.version" android:value="2" />
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyAmxxxxxxxxxxxxxxxxxxxxxxxxx" />
</application>
UPDATE:
I added checking for google services availability as is shown here and it returns ConnectionResult.Success.
GoogleApiAvailability gaa=GoogleApiAvailability.Instance;
int resultCode=gaa.IsGooglePlayServicesAvailable(context);
if (resultCode ==ConnectionResult.Success)
{
...
}
After reading this question I changed the line
<meta-data android:name="com.google.android.gms.version" android:value="2" />
for
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
Nothing changed.
In you I would try to create LinearLayout and MapView inside your layout axml (if you can) to be sure that it's as it should be, getting the map when it's time to call the GetMapAsync using FindViewById<MapView> on it's resource id.
Setup of the map and the call of GetMapAsync should be done inside OnStart and OnResume.
After you created your IOnMapReadyCallback you could also try to add directly an event handler like this to see if it's called:
mapReader.MapReady += (sender, args) => { };
Try also your code both on a device with one of the latest Android version but also on a Android 5.x so you are sure that you haven't problem with requesting of permissions at runtime introduced with Android 6.0.

Google Maps [API] view is gray on my mobile device

Hi guyes I really need your help.
I've read the instructions on how to use Google API on this link:
https://developers.google.com/maps/documentation/android/start
I've followed all seven steps but I couldn't display Google Map in my app.
[I have already got tired from trying to get the Emulator to work so I've decided to generate an APK file instead and send it to my smartphone]
After intalling the app, once I open it I can only see a grey screen and 'Google' signeture at the bottem left of the screen. that's all, nothing happens.
The app works fine when I connect my device and use the debug mode.
I've tried installing it Via the adb but it still didn't work.
I've been following many articles for the last three days regarding this issue but no matter what I've tried it just didn't work.
What am I doing wrong?
Here is my code:
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="josh.com.googlemapslast" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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_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" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<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>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIza**********"/>
</application>
</manifest>
Main Activity Java:
package josh.com.googlemap2;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap map) {
// Add a marker in Sydney, Australia, and move the camera.
LatLng sydney = new LatLng(-34, 151);
map.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
map.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
Main Activity Layout:
<fragment 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"
android:id="#+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
Goole_Maps_api.xml
<string name="google_maps_key" translatable="false" templateMergeStrategy="preserve">
AIzaSyAnvkHr4CGYzmROT*******
</string>
</resources>
Alright, There are two things that you should note if you encounter a similar situation:
1) There are two google maps api.xml files, one for release and one for debug.
you can find them on these paths:
\app\src\release\res\values
\app\src\debug\res\values
Make sure to add your Google Map Key on both files!
2) I didn't find this information on official source but you need to generate another SHA1 key for the release file in addition to your regular SHA1 key.
You must have two SHA1 keys, one for the debug version and one for the release version.
Once you have the new key for the release file, add it to your Google API Console exisiting key at the second row.

Google play services in google maps

I have this mistake when I try to run my application:
"this app won't run unless you update google play services"
I have tried many things that are here, but nothing.
I've generated the application with the assistance of Android Studio.
I have download the rev 22 to Google Play Services and rev 12 of Google Play services for Froyo
My proyect is Api 17 and when I run the project in the phone, it's display ok.
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemaps.googlemaps" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.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>
</manifest>
MainActivity
package com.example.googlemaps.googlemaps;
import android.app.Dialog;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMyLocationChangeListener {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
if(status!=ConnectionResult.SUCCESS){
int requestCode=10;
Dialog dialog=GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
dialog.show();
}
else{
SupportMapFragment fm=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
mMap=fm.getMap();
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(this);
}
}
#Override
public void onMyLocationChange(Location location) {
// TODO Auto-generated method stub
// TextView tvLocation=(TextView)findViewById(R.id.textView1);
double latitude=location.getLatitude();
double longitude=location.getLongitude();
LatLng latLng=new LatLng(latitude,longitude);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
// tvLocation.setText("Latitude:"+latitude+", Longitude:"+longitude);
}
}
The error you are getting is because you are not running the updated version of Google Play Services that is 6.5. Make sure you have added that in your build.gradle file as dependencies. Add the following lines.
dependencies {
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.android.gms:play-services:6.5.87'
}
If you are using your app just for maps you can selectively add:
com.google.android.gms:play-services-maps:6.5.87
Please refer to this documentation for more details.
Assuming that you have replaced the API_KEY in your manifest file with your actual API key, you should be able to see the maps running on your app without getting any error.
For more details read official documentation on Google Play Services 6.5.
Good news for you. Google now provides a standalone library for maps which, at the time of writing this post, is in beta
The Maps SDK for Android is now distributed via a standalone static library. Previously, the Maps SDK for Android was made available as part of Google Play services
https://developers.google.com/maps/documentation/android-sdk/v3-client-migration

Adding a map to a class

Hello I would like to add a map (static image) to a page. I will try to explain what I have done so far and what my problem is as good as I can in case someone else has the same problem in the future.
I have done the following:
1. Updated the Manifest file with permissions/API (got the API (from Google API Console) key using the SHA1 from Eclipse using Window > Preferences > Build
2. Updated the XML file of the class where the map will be added
3. Added some Java code into the classes Java file
Below is the Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.learn2develop.lapmaster"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/lapmaster_icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="net.learn2develop.lapmaster.LapMasterActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="Logged In"
android:name=".UserLoggedInScreen" >
<intent-filter >
<action android:name="net.learn2develop.UserLoggedInScreen" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:label="Track Listing"
android:name=".ListOfCircuits" >
<intent-filter >
<action android:name="net.learn2develop.ListOfCircuits" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:label="Snetterton"
android:name=".Snetterton" >
<intent-filter >
<action android:name="net.learn2develop.Snetterton" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Other activities -->
<!-- Google Play Services -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="Various" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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>
</manifest>
Below is the class XML:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment" />
Below is the class Java:
import android.app.Activity;
import android.os.Bundle;
import com.google.android.gms.maps.SupportMapFragment;
public class Snetterton extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.snetterton);
SupportMapFragment map = ((SupportMapFragment)
.getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
}
}
As you can probably guess from my code I am creating an app for race tracks. In the class called Snetterton I want to add a picture (Google maps) of Snetterton.
To get to this page the user:
1. Starts at a login screen (LapMasterActivity)
2. Is greeted with a home page (UserLoggedInScreen)
3. Goes to a page with a list of tracks (for now it is just Snetterton) (ListOFTracks)
4. When the user clicks the button for Snetterton it takes them to the Snetterton class' page where I would like the map. (Snetterton).
In my code only ((SupportMapFragment) is underlined.
Thanks.
Answered this myself (eventually):
Code I used is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayoutFragment"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
public class TopGear extends Activity
{
private GoogleMap aMap;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.top_gear);
aMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
aMap.getUiSettings().setAllGesturesEnabled(false);
LatLng topGearTrack = new LatLng(51.116492, -0.541767);
aMap.setMyLocationEnabled(true);
aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(topGearTrack, 15));
aMap.addMarker(new MarkerOptions()
.title("Top Gear")
.snippet("Welcome to the famous Top Gear circuit.")
.position(topGearTrack));
aMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
setUpMapIfNeeded();
}
public void setUpMapIfNeeded()
{
//Do a null check to confirm that we have not already instantiated the map.
if (aMap == null)
{
aMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
//Check if we were successful in obtaining the map.
if (aMap != null)
{
//The Map is verified. It is now safe to manipulate the map.
}
}
}
}