Android GoogleMap or SupportMapFragment - null pointer exception - android

In my app i showing google map version2 in a fragment. but i get Null pointer exception
at
mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
This is my full code:
public class SearchResultMap extends Fragment{
private GoogleMap mMap;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.map, container, false);
mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); // line no : 28
mMap.addMarker(new MarkerOptions() .position(new LatLng(xxxxxx,xxxxxx)) .title("Current Location")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ball_pointer))
.snippet("xxxxx"));
return fragmentView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Manifest xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fssd.spot"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission
android:name="com.fssd.spot.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.fssd.spot.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.fssd.spot.SplashScreen"
android:theme="#android:style/Theme.Light.NoTitleBar"
android:noHistory="true"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.fssd.spot.MainActivity"
android:screenOrientation="portrait"
android:theme="#style/tabTheme" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxxxxxxxxxxxxxxxxxxxxxxx" />
</application>
</manifest>
whole log cat:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.fssd.spot.search.SearchResultMap.onCreateView(SearchResultMap.java:28)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
at android.support.v4.app.Fragment.performStart(Fragment.java:1499)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:957)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
at dalvik.system.NativeStart.main(Native Method)

None of these solutions fixed it for me -- but this answer from Mark Murphy (https://code.google.com/p/android/issues/detail?id=77714) did:
Use getChildFragmentManager() to access a nested fragment, not getFragmentManager(). http://developer.android.com/reference/android/support/v4/app/Fragment.html#getChildFragmentManager%28%29
i.e.:
public class MapFragment extends Fragment {
private GoogleMap map;
private SupportMapFragment mapFragment;
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle data) {
View view = inflater.inflate(R.layout.map_view, vg, false);
FragmentManager fm = getChildFragmentManager();
mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
mapFragment.getMapAsync(new OnMapReadyCallback() {
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
initMap();
}
});
}
}

try to change this:
FragmentManager myFM = getActivity().getSupportFragmentManager();
final SupportMapFragment myMAPF = (SupportMapFragment) myFM
.findFragmentById(R.id.map);

Try changing to
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
You're using getFragmentManager(), but casting it to SupportMapFragment
You also need to extend FragmentActivity: http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html

mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if(map != null) {
mMap.addMarker(new MarkerOptions() .position(new LatLng(xxxxxx,xxxxxx)) .title("Current Location")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ball_pointer))
.snippet("xxxxx"));
}
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxxxxxxxxxxxxxxxxxxxxxxx" />
place your google api key here. You get from google Read the documentation.

After hours of search, I did in this way:
public class MapFragment extends Fragment {
private GoogleMap map;
SupportMapFragment mapFrag;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
setHasOptionsMenu(true);
View rootView = inflater.inflate(R.layout.fragment_map, container,
false);
fm = getActivity().getSupportFragmentManager();
map = ((SupportMapFragment) fm.findFragmentById(R.id.map)).getMap();
return rootView;
}
}

Try to add tools:context=".YourActivity" to the fragment xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".YourActivity" />

May be above answers work but what work for me is this.
{
SupportPlaceAutocompleteFragment supportPlaceAutocompleteFragmentSearch;
FragmentManager fragmentManagerSearch = getChildFragmentManager();
supportPlaceAutocompleteFragmentSearch =
(SupportPlaceAutocompleteFragment)
fragmentManagerSearch.findFragmentById(R.id.place_autocomplete_fragment_search);
}

if you developed your code with Kotlin, and want to add MapFragment to another Fragment you need to use SupportMapFragment but if you do it, you will get null exception so you should use childFragmentManager rather than supportFragmentManager and the code should be in onCreateView not in onCreate just add below code:
private lateinit var mapFragment : SupportMapFragment
mapFragment = childFragmentManager.findFragmentById(R.id.mapview) as SupportMapFragment
mapFragment.getMapAsync { googleMap ->
this.googleMap = googleMap
setGoogleMapSettings()
}

Related

Android Google Map in Fragment always show the SupportMapFragment was null

I want build a android map application and Launch app will got null from SupportMapFragment(said my supportMapFragment is null), if you can point me where is my mistake, please help, many thanks.
code, XML and manifest as below.
By the way, the MapFragment is come from BottomNavigationView tab
R.id.navigation_map -> {
val mapFragment = MapFragment()
mapFragment.setArguments(intent.extras)
supportFragmentManager.beginTransaction()
.add(R.id.fragment_container, mapFragment).commit()
return#OnNavigationItemSelectedListener true
MapFragment.java
public class MapFragment extends Fragment implements OnMapReadyCallback {
private View rootView;
private AppCompatActivity context;
private SupportMapFragment supportMapFragment;
private GoogleMap map;
public MapFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_map, container, false);
context = (AppCompatActivity) getActivity();
supportMapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.fragmentmap);
if (supportMapFragment != null) {
supportMapFragment.getMapAsync(this);
getFragmentManager().beginTransaction()
.replace(R.id.fragment_container, supportMapFragment).commit();
} else {
Toast.makeText(context, "Error - Map Fragment was null!!", Toast.LENGTH_SHORT).show();
}
return rootView;
}
#Override
public void onMapReady(GoogleMap googleMap) {
}
}
fragment_map.xml
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragmentmap"
tools:context=".MainActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</android.support.design.widget.CoordinatorLayout>
Manifests
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
</application>
If you are using fragment inside another fragment then better to use getChildFragmentManager as it return a private FragmentManager for placing and managing Fragments inside of this Fragment.
supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.fragmentmap);
Reference: getChildFragmentManager

Map content doesn't show up using MapView from a fragment

I'm struggling with Google Maps. I'm using a drawer with fragments so I have to use MapView afaik (no fragment call from a fragment).
First I had an authorization failure, so I re-generated a key, ran keytool, my google_maps_api.xml is correct so the message about the authorization issue is gone. Still I see no content and I have a strange message about StrictModeDiskReadViolation.
this is what I get in logcat:
12-15 20:41:14.842 703-703/com.bernard_zelmans.check I/Google Maps Android API: Google Play services package version: 10084448
12-15 20:41:14.842 703-703/com.bernard_zelmans.check W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation
12-15 20:41:14.842 703-703/com.bernard_zelmans.check W/f: Suppressed StrictMode policy violation: StrictModeDiskWriteViolation
12-15 20:41:14.882 703-703/com.bernard_zelmans.check W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation
12-15 20:41:14.882 703-25272/com.bernard_zelmans.check W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation
12-15 20:41:14.882 703-703/com.bernard_zelmans.check W/f: Suppressed StrictMode policy violation: StrictModeDiskWriteViolation
12-15 20:41:14.882 703-25274/com.bernard_zelmans.check W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation
And here is a snapshot from my app:
Here is my code:
public class PortFragment extends Fragment implements OnMapReadyCallback {
Context context;
View view;
MapView mapView;
GoogleMap map;
LatLng CENTER = null;
public LocationManager locationManager;
private GoogleMap googleMap;
double longitude = 0;
double latitude = 0;
public PortFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.portscan, null);
MapsInitializer.initialize(this.getActivity());
mapView = (MapView) view.findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
return (view);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
context = getActivity().getApplicationContext();
}
#Override
public void onMapReady(GoogleMap map) {
setUpMap();
googleMap = map;
LatLng sydney = new LatLng(-34, 151);
googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
public void setUpMap() {
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setMyLocationEnabled(true);
googleMap.setTrafficEnabled(true);
googleMap.setIndoorEnabled(true);
}
Below is my manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bernard_zelmans.check">
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.STORAGE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:configChanges="keyboardHidden|screenSize"
android:icon="#mipmap/ic_launcher"
android:label="Test"
android:logo="#drawable/ic_launcher"
android:screenOrientation="portrait"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activities.Settings" />
<activity android:name=".Activities.About" />
<service
android:name=".Service"
android:exported="false" />
<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="#string/google_maps_key" />
</application>
</manifest>
If you want to add a map in your fragment you should not add create it like this. You should add it in your layout, as you did, but then in onCreateView() method you should get the view and directly call getMapAsync on it :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.portscan, container);
MapFragment mapFragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return (view);
}
And in layout :
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.google.android.gms.maps.MapFragment"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
As you see, you first add map fragment in layout and then you handle it in your own fragment.
You should take a look at android gmap documentation here :
https://developers.google.com/maps/documentation/android-api/map

getMap() is null in SupportMapFragment in google map in android fragment

I am using google map in a fragment and getting null when calling getMap(), this is my code:
In my MapViewFragment
private GoogleMap mGoogleMap;
I am getting null at this line, in onCreate() method...
mGoogleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
In fragment_map_view file
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
The getMap method is deprecated. You should use getMapAsync:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
}
#Override
public void onMapReady(final GoogleMap googleMap) {
mGoogleMap = googleMap;
// Do something with your map
}
}
did you add the permissions and google API key ?
here is my code from project call this function in OnCreate()
private void CheckGoogleMap()
{
try {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().
findFragmentById(R.id.map)).getMap();
}
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
catch (Exception e) {
e.printStackTrace();
}
}
in XML file
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
in Manifest file
<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.example.googlemaps.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET"/>
<permission android:name="com.example.googlemaps.permission.MAPS_RECEIVE" />
<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" />
before closing of application TAG in Manifest file add this
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="YOUR API KEY" />
in Gradle add
compile 'com.google.android.gms:play-services:8.1.0'
I got the reason for it, I think I should post for future readers......
Actually as I GoogleMap was called in a fragment so, in the line..
mGoogleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
instead of
getFragmentManager(), getSupportFragmentManager() should be used in fragments.
Corrected one-
mGoogleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

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);
}
};

how to use maps inside the fragment

i can show plain guide in inside part i have to add marker to specific geolocation
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="100dp"
android:name="com.google.android.gms.maps.SupportMapFragment"
/>
I don't did anything in java code however i extended my class with Fragment
Don't forget to add google-play-service-lib as a library project and
it should sharing the same workspace of your project
Java code
public class SomeFragment extends Fragment {
MapView mapView;
GoogleMap map;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.some_layout, container, false);
// Gets the MapView from the XML layout and creates it
mapView = (MapView) v.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
map = mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
// Needs to call MapsInitializer before doing any CameraUpdateFactory calls
try {
MapsInitializer.initialize(this.getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
// Updates the location and zoom of the MapView
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
map.animateCamera(cameraUpdate);
return v;
}
#Override
public void onResume() {
mapView.onResume();
super.onResume();
}
#Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
#Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
Layout
<LinearLayout 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" >
<com.google.android.gms.maps.MapView android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<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.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<permission
android:name="com.example.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.permission.MAPS_RECEIVE"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="your_key"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".HomeActivity"
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>
Edit: After your edit new question arise.
After initializing map check if it is not null from official documentation
if(map != null)
{
Marker marker = map.addMarker(new MarkerOptions()
.position(new LatLng(37.7750, 122.4183))
.title("San Francisco")
.snippet("Population: 776733"));
}
In your layout, replace your code with this one :
<RelativeLayout
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="100dp">
</RelativeLayout>
and in your code :
private SupportMapFragment supportMapFragment;
private GoogleMap map;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
supportMapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
if (supportMapFragment == null) {
supportMapFragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map, supportMapFragment).commit();
}
}
#Override
public void onResume() {
super.onResume();
...
map = supportMapFragment.getMap();
...
}

Categories

Resources