Google maps not created, callback function never runs - google-maps

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.

Related

Can't open .xlsx file from aspose cells if it wasn't created using aspose cells

So I'm working on an app for android and I've gotten it to work as long as it's been created using aspose cells. Here is my code:
class Home: Fragment(R.layout.fragment_home) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view: View = inflater.inflate(R.layout.fragment_home, container, false)
val load = view.findViewById<Button>(R.id.button3)
val new = view.findViewById<Button>(R.id.button4)
val editText = view.findViewById<EditText>(R.id.editTextTextPersonName)
new.setOnClickListener(View.OnClickListener {
Excel.name = "${Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS)}/${editText.text.toString()}.xlsx"
Excel.workbook = Workbook()
Excel.workbook!!.save(Excel.name)
println("Created: ${Excel.workbook}")
})
load.setOnClickListener(View.OnClickListener {
Excel.workbook = Workbook("${Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS)}/${editText.text.toString()}.xlsx")
Excel.name = "${Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS)}/${editText.text.toString()}.xlsx"
Excel.workbook!!.save("${Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS)}/${editText.text.toString()}.xlsx")
println("Loaded: ${Excel.workbook}")
})
return view
}
}
the problem is when I try to download a .xlsx file and load it, it gives me this error:
Caused by: java.io.FileNotFoundException: /storage/emulated/0/Download/ECR.xlsx: open failed: EACCES (Permission denied)
I'm not sure why this is happening. Any help would be awesome! Thanks!
EDIT:
Here's my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.FRCScout22"
tools:targetApi="31">
<activity
android:name=".DropDownActivity"
android:exported="false">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>

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 [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.

"Error inflating class fragment" with google map on xamarin

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.

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