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);
}
};
Related
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"/>
I'm trying to get my current location on google map but it's giving me a blank map.
this is my fragment
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationSource.OnLocationChangedListener, GoogleMap.OnMapClickListener {
private GoogleMap mMap;
private LatLng userLocation;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
public LocationManager mLocationManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
#Override
public void onLocationChanged(Location location) {
userLocation = new LatLng(location.getAltitude(), location.getLongitude());
Toast.makeText(this, "found you...", Toast.LENGTH_SHORT).show();
MarkerOptions markerOptions= new MarkerOptions();
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 10));
mMap.addMarker(new MarkerOptions().position(userLocation).title("you are here"));
}
#Override
public void onMapClick(LatLng latLng) {
}
}
then that's my manifest
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="25" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyBuSE2O0siczDYt3oh3FVUgcO_MZs4-8FQ" />
<!-- To access Google+ APIs: -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.example.liadiluz.finalproject.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:name="android.support.multidex.MultiDexApplication"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".services.MyIntentService"
android:exported="false" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<activity
android:name=".Activities.MapsActivity"
android:label="#string/title_activity_maps2"></activity>
</application>
and that's the layout file
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.liadiluz.finalproject.Activities.MapsActivity" />
</RelativeLayout>
I'm trying to run simple demo app of Google Maps. I've referred standard article.
But I'm getting Error inflating class fragment exception.
I've updated mt manifest properly. Registered for Map API key. Install google-play-services and linked it to my current project. Extended my activity from FragmentActivity, still it doesn't work.
Am I doing anything wrong?
I also referred Error inflating class fragment but to no avail.
Any help appreciated.
My code is as follows:
activity_main.xml
<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.MapFragment" />
</RelativeLayout>
MainActivity.java
package com.android.googlemapsdemo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
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 FragmentActivity {
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)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
} else {
map = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
if (map == null) {
Toast.makeText(MainActivity.this, "Unable to generate map",
Toast.LENGTH_SHORT).show();
}
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.googlemapsdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<permission
android:name="com.android.googlemapsdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.android.googlemapsdemo.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" >
<activity
android:name="com.android.googlemapsdemo.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="AIzaSyAT9_V5YI-7CMf_Yta0Y_zeDfPZeH5XFL0" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
Change this
<uses-sdk
android:minSdkVersion="11"
to
<uses-sdk
android:minSdkVersion="12"
If you have 11 you will be using SupportMapFragment. Consider this
https://developer.android.com/about/dashboards/index.html?utm_source=ausdroid.net
Looking at the link it is better to change to 12
Change this
public class MainActivity extends FragmentActivity
to
public class MainActivity extends Activity
Also getMap() can return null. Better check the availability of google map services.
You are also missing
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
in manifest file
https://developers.google.com/maps/documentation/android/start#getting_the_google_maps_android_api_v2
You can also remove
<permission
android:name="com.android.googlemapsdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
and this
<uses-permission android:name="com.android.googlemapsdemo.permission.MAPS_RECEIVE" />
I think you should change this
class="com.google.android.gms.maps.MapFragment"
to
class="com.google.android.gms.maps.SupportMapFragment"
Because your minsdk="11"
also change this
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
to
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
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
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.