Android : Google Maps API V2 displays black screen ECLIPSE - android

I am trying to show my current location on google maps but it displays black screen in the map fragment.
my manifest file
<?xml version="1.1" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.edraky.tasksheet"
android:versionCode="13"
android:versionName="6.7" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="26" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<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-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.edraky.tasksheet.MAPS_RECEIVE" />
<permission
android:name="com.edraky.tasksheet.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.edraky.tasksheet.permission.MAPS_RECEIVE" />
<application
android:allowBackup="true"
android:icon="#drawable/transparentlogo"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".SplashScreenActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login" >
</activity>
<activity
android:name=".ContactUs_Activity"
android:label="#string/title_activity_contact_us" >
</activity>
<activity
android:name=".AboutUs_Activity"
android:label="#string/title_activity_about_us_"
android:noHistory="false" >
</activity>
<activity
android:name=".WelcomePage"
android:label="#string/title_activity_welcome_page" >
<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="AIzaSyDpP-HMGNQCnFPnzODgTiJFXs00yirfhHE" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
ContactUs Layout ( layout displaying the map )
Contact us layout
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.edraky.tasksheet.ContactUsActivity">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_marginTop="40dp"
android:layout_height="260dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:textStyle="bold|italic"
android:textSize="11dp"
android:textIsSelectable="true" />
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignParentBottom="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="24dp"
class="com.google.android.gms.maps.SupportMapFragment"
android:scrollbars="vertical" />
<Button
android:id="#+id/homebutton"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/contactusicon" />
</RelativeLayout>
contact us activity class
public class ContactUs_Activity extends FragmentActivity {
GoogleMap mMap;
GPSTracker gps;
Double currLat;
Double currLong;
GMapV2Direction gmap;
Button homebutton;
TextView view1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_us);
homebutton = (Button) findViewById(R.id.homebutton);
view1 = (TextView) findViewById(R.id.textView1);
view1.setText("Text Shown");
homebutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = mapFragment.getMap();
gps = new GPSTracker(this);
currLat = gps.getLatitude();
currLong = gps.getLongitude();
LatLng sydney = new LatLng(currLat, currLong);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
i included all the libraries and generated the API key
output is shown below
no error message is logged. what could possibly go wrong in displaying the map ?
Note : same code is working on another application with different API key ofcourse and map is displayed correctly.
IMPORTANT NOTE:The app was already published on google play store before adding the Maps SDK API,so is there any modifications that I should do considering that or its irrelative?

Go to console.google.com
And generate a new API key with package name(com.package.app) same as in your mobile app and SHA1.
Get the new API key, replace in the code. Uninstall the app and re-run.

Okey , so i am not using FragmentActivity, and this may not directly tell the solution.
But this code definitely works and shows map using android.support.v4.app.Fragment.
import android.support.v4.app.Fragment;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
public class OrderMapFrag extends Fragment implements OnMapReadyCallback{
private GoogleMap mGoogleMap;
private MapView mMapView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_order_map, container, false);
........
........
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mMapView.getMapAsync(this);
return view;
}
#Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
//setting initial elements in map if required after this , as map got initialized properly
}
}
But I am not using exact same elements and permissions you use in Manifest. For me these are the used ones.
<uses-permission android:name="android.permission.INTERNET" />
<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" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="MY API KEY"/>

Related

Can't setup Google map v2

I am trying over a week to setup my Google Map v2. but its not working for me.I tried answers from questions related to it but no use.i couldn't track the exact problem.please help.
Here is my xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
In my map.class I have
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.*;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.SupportMapFragment;
import android.app.Activity;
import android.app.Dialog;
import android.support.v4.app.Fragment;
import android.widget.Toast;
import android.os.Bundle;
public class Map extends Activity {
private GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
}
My API key is correct and I imported google play services library as per the directions.In my manifest file
<permission
android:name="com.example.sellatease.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.sellatease.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="21"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<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" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
and
<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="AIzaSyCwHLxfMRBnD_MJDQDV9IYBZTxbkCVlZsA" />
This is my error log
And in console I got can not find googleplayserviceslib.apk error too.
new error log
you need to extend FragmentActivity/AppCompatActivity if you are using SupportMapFragment.
public class Map extends FragmentActivity/AppCompatActivity {
And
FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment mySupportMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
googleMap = mySupportMapFragment.getMap();
You can check Android Map V2
Try this
googleMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
Try this...
activity_map.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
Map.java
public class Map extends android.support.v4.app.FragmentActivity {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
setUpMapIfNeeded();
}
#Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
if (mMap != null) {
setUpMap();
}
}
}
AndroidManifest.xml
....
<permission
android:name="com.example.sellatease.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-sdk
android:minSdkVersion="21"
android:targetSdkVersion="21" />
<uses-permission
android:name="com.example.sellatease.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<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" />
<!-- Maps API needs OpenGL ES 2.0. -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
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="AIzaSyCwHLxfMRBnD_MJDQDV9IYBZTxbkCVlZsA" />
<activity
android:name=".Map"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
....
UPDATE
Create Google map programatically.
activity_map.xml
<FrameLayout
android:gravity="top"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/mapContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
Map.java
public class Map extends AppCompatActivity{
private GoogleMap mMap;
private SupportMapFragment mMapFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
mMapFragment = SupportMapFragment.newInstance();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.mapContainer, mMapFragment, "map");
fragmentTransaction.commit();
mMapFragment.getMapAsync(mapReadyCallback);
}
private OnMapReadyCallback mapReadyCallback = new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mMap.setMyLocationEnabled(true);
mMap.setTrafficEnabled(true);
mMap.setIndoorEnabled(true);
mMap.setBuildingsEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
}
};

Google Maps v2 in a Android Studio app requires Google Play Services update even though it is up-to date

I have a navigation drawer with fragments for each page. And in one of my fragments i want to display a mapview. But not in full screen, i want it to be like the top half of screen and below that i will give some contact information for a specific coordinate.
I tried too many tutorial but i couldn't get this working.
The Page XML which Map is supposed to be displayed
<?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="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/map_background"
tools:context="com.corvega.ztbb.IletisimFragment">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ContactMapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"/>
</RelativeLayout>
Behind the Map's page JAVA
public class IletisimFragment extends Fragment {
public MapView mapView;
private static GoogleMap googleMap;
public IletisimFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_iletisim, container, false);
if (googleMap != null)
setUpMap();
if (googleMap == null)
{
googleMap = ((MapFragment)MainActivity.fragmentManager.findFragmentById(R.id.ContactMapView)).getMap();
if (googleMap != null)
{
setUpMap();
}
}
return rootView;
}
private static void setUpMap()
{
googleMap.setMyLocationEnabled(true);
googleMap.addMarker(new MarkerOptions().position(new LatLng(41.009471, 28.916134)).title("baslik"));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(41.009471, 28.916134), 12.0f));
}
#Override
public void onDestroyView()
{
super.onDestroyView();
if (googleMap != null)
{
MainActivity.fragmentManager.beginTransaction()
.remove(MainActivity.fragmentManager.findFragmentById(R.id.ContactMapView))
.commit();
googleMap = null;
}
}
}
My AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<permission android:name="com.arshad.map.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
<uses-permission android:name="com.example.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>
<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_icon_name"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name" />
<activity
android:name=".SplashScreen"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_icon_name"
android:noHistory="true"
android:theme="#android:style/Theme.NoTitleBar">
<intent-filter android:label="#string/app_icon_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<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="my api key was here"/>
</application>
</manifest>
dependencies in Build.Gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:20.0.0'
compile 'com.google.android.gms:play-services:5.2.08'
}
However my final result is this. I am sure that Google Play Services is up-to date on the phone. So do you have any suggestions?
Change your gradle file to
compile 'com.google.android.gms:play-services:5.0.89'
unless if you're targeting L Preview

Android Google Map classNotFound exception

Basically I am trying to make an app which displays a Google Map.Here is my code.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<com.google.android.maps.MapView
android:id="#+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="my key goes here"
/>
</LinearLayout>
MainActivity class...
import com.google.android.maps.MapActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends MapActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
When I run it,I get the following message
06-22 15:54:03.622: E/AndroidRuntime(26187): FATAL EXCEPTION: main
06-22 15:54:03.622: E/AndroidRuntime(26187): java.lang.RuntimeException:
Unable to instantiate activity
ComponentInfo{com.example.maptutorial/com.example.maptutorial.MainActivity}:
java.lang.ClassNotFoundException: com.example.maptutorial.MainActivity
Perhaps I might be doing something wrong in my Manifest File,and can not see it..
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.maptutorial"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.maptutorial.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>
</application>
</manifest>
you forgot to add uses-library in your manifest file,in this case
<application>
...
<uses-library android:name="com.google.android.maps"
android:required="true" />
...
</application>
I think you are using MAP API version 1 which is deprecated.
Why Cant you use API 2 either Fragment or support-fragment (https://developers.google.com/maps/documentation/android/start)
<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.MapFragment"/>
Manifest file
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyC8qEhdHVd3ZtxCR0549UqwnM72h0jgoMY" />
Basically I had to use the use the SupportMapFragment like this to make it work for android 2.3 .Also had to create a new API key..
<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>
And an sample of my java code is this.
public class EmergencyMap extends FragmentActivity {
static final LatLng ASTYNOMIA = new LatLng(39.620530, 22.401728);
static final LatLng PYROSVESTIKH = new LatLng(39.632652, 22.398704);
static final LatLng genikonosokomeio = new LatLng(39.642081, 22.422350);
static final LatLng panosokomeio = new LatLng(39.609993, 22.385722);
private GoogleMap map;
final int RQS_GooglePlayServices = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emergency_map);
map = ((SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
map.addMarker(new MarkerOptions().position(ASTYNOMIA).title("ΑΣΤΥΝΟΜΙΑ"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(ASTYNOMIA,15));
map.animateCamera(CameraUpdateFactory.zoomTo(20),2000,null);
map.addMarker(new MarkerOptions().position(PYROSVESTIKH).title("ΠΥΡΟΣΒΕΣΤΙΚΗ"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(PYROSVESTIKH,15));
map.animateCamera(CameraUpdateFactory.zoomTo(20),2000,null);
map.addMarker(new MarkerOptions().position(genikonosokomeio).title("ΓΕΝΙΚΟ
ΝΟΣΟΚΟΜΕΙΟ"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(genikonosokomeio,15));
map.animateCamera(CameraUpdateFactory.zoomTo(20),2000,null);
map.addMarker(new MarkerOptions().position(panosokomeio).title("ΠΑΝΕΠΙΣΤΗΜΙΑΚΟ
ΝΟΣΟΚΟΜΕΙΟ"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(panosokomeio,15));
map.animateCamera(CameraUpdateFactory.zoomTo(20),2000,null);
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
protected void onResume() {
super.onResume();
int resultCode =
GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS){
Toast.makeText(getApplicationContext(),
"Το κινητό σας υποστηρίζει GooglePlayServices",
Toast.LENGTH_LONG).show();
}else{
GooglePlayServicesUtil.getErrorDialog(resultCode, this,RQS_GooglePlayServices);
}
}
}
It is Google Map API 2. Here is a part of my manifest file.
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<permission
android:name="com.example.larissaguide.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="larissa.app.permission.MAPS_RECEIVE" />
<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" />
<!--
The following two permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<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="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="larissa.app.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_KEY_GOES_HERE" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />

android error: map cannot be resolved or is not a field

I'm following this tutorial to learn how to use googleMaps API.
But in MainAntivity.java it gives me an error I'm not sure how to solve:
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
I get map cannot be resolved or is not a field but it reffers to: d(R.id.map)
Any ideas why map in R.id.map is failing??
EDIT :
MainActivity.java
package com.example.mapsv2;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends Activity {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
if (map!=null){
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG).title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
}
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="19" />
<permission
android:name="com.vogella.android.locationapi.maps.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.example.mapsv2.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" />
<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.mapsv2.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="your_apikey" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
Have you implemented the Fragment in XML file? Refer this blog
// try this way,hope this will help you...
1.Replace this code
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
With this
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
2.Add this permission in AndroidManifest file
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Note : if you are using latest google-play-services jar for map then you have to also define below meta-data in your AndroidManifest file
<meta-data android:name="com.google.android.gms.version"
android:value="4030500" />
Try using below code
xml
<fragment
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="com.google.android.gms.maps.SupportMapFragment" />
java
public class MainActivity extends FragmentActivity {
GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment supportMapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
// Getting a reference to the map
googleMap = supportMapFragment.getMap();
}
}
Also your app should extend FragmentActivity.
Just add class="com.google.android.gms.maps.MapFragment" activity_maps.xml

Can´t run my app with google maps api v2

I tried everything, I researched and I can't get run my application. Each time I Run my application on my device get the message: "Sorry, Map has been has been interrupted". I done all the steps from this page https://developers.google.com/maps/documentation/android/start?hl=pl, and follow many tutorial and nothing. I hope you can save me.
Manifeste File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.map"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.example.map.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.map.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.map.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>
<!-- Goolge Maps API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBEGWDP9v4HepDF9A1NmSfFYjfdU2Jezm4" />
</application>
</manifest>
XML File:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_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"/>
And my MainActivity:
package com.example.map;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
// Google Map
private GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
#Override
protected void onResume() {
super.onResume();
initilizeMap();
}
}
Please Help me!!!!
try this :
class="com.google.android.gms.maps.SupportMapFragment"
on your activity :
SupportMapFragment mapfragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.showmap);
I think your problem in
android:name="com.google.android.gms.maps.MapFragment"
try instead of this, in the way:
class="com.google.android.gms.maps.MapFragment"
P.S. sometimes problem with maps is in not available Google Play Service on device.
(GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS)
must be true.

Categories

Resources