i had a map in my android app.....me and my friend is working on that he had created a map nd it is display in his emalutor...when i import same project in my eclipse ..nd run it gives me error that camera update factory is not initilze...but my partner is working on same map....i cant understand what is the problem in my emalutor or eclipse or any other reason..if any one know then help me
my xml file is
<LinearLayout 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:orientation="vertical" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
i had called this from my map activity
MapsInitializer.initialize(this);
getLayoutInflater().inflate(R.layout.activity_main_map, frameLayout);
googleMap = supportMapFragment.getMap();
googleMap.animateCamera(CameraUpdateFactory.zoomTo(20.0f));
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
googleMap.getUiSettings().setZoomGesturesEnabled(true);
The getMap() method is async , your next line will be executed before the map is ready , do this:
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.animateCamera(CameraUpdateFactory.zoomTo(20.0f));
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
googleMap.getUiSettings().setZoomGesturesEnabled(true);
}
}
Related
Iam practicing on Google Maps for android development. I have followed the steps as in https://developers.google.com/maps/documentation/android-api/map-with-marker
While when playing the app I got the screen like this below, no marker and the map cannot show any thing
My code as following:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
// Add a marker in Sydney, Australia,
// and move the map's camera to the same location.
LatLng sydney = new LatLng(-33.852, 151.211);
googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
******** . XML Layout ************
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.ae.map_04.MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
This is most likely an API Key issue. You can find some guidance here: Android Studio - Google map still blank on real Android device on release apk
If that does not work, try a new API Key.
I move my application eclipse to android studio. My code works perfect on Eclipse but on Android studio
mapView = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
return null
my xml is here
<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"
android:orientation="vertical"
xmlns:ads="http://schemas.android.com/apk/res-auto"
>
<com.google.android.gms.ads.AdView
android:id="#+id/adMob"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="#string/admob_unit_id" />
<fragment
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
why it return null?
I searched and compileSdkVersion 21 cause it. But if I change it 19 still return null
getMap() is not guaranteed to return a map instance. Use getMapAsync(OnMapReadyCallback) where the callback will be invoked once the map is ready and you're guaranteed it won't be null. This is available since Google Play Services library v6.5.
When using this approach get rid of your mapView variable, the callback will receive a map reference anyway. Also note that SupportMapFragment is not MapView is not GoogleMap (I'm referring to the misnamed variable here, be careful.).
EDIT: Suggested solution:
public class MainActivity extends ActionBarActivity {
SupportMapFragment mMapFragment;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
mMapFragment = getSupportFragmentManager().findFragmentById(R.id.map);
// etc.
}
public void doSomethingWithMap() {
mMapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
// do whatever you need with the map
}
});
}
}
The point is when you need to access the map, you ask the fragment for it, and it will be delivered to you via a callback.
I am having a FragmentActivity, where I have successfully implemented a Google Map through my XML with this code:
<fragment
android:id="#+id/googleMap"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
However, if I try to initialize the map to find a persons location with this code:
GoogleMap googleMap;
googleMap = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.googleMap))).getMap();
It is throwing a null pointer exception, as I have understood from other questions regarding this error, it shouldn't be a problem initializing the map when it is created in the XML file. And yes I have used
setContentView(R.layout.map);
Before I initialize the map.
You use the Support Fragment So in your XML File Add
this Line In Fragment
class="com.google.android.gms.maps.SupportMapFragment"
like belove.
<fragment
android:id="#+id/googleMap"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
/>
and Put This
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.googleMap)).getMap();
The error is due to incorrect variable initialization try to change your code as shown below,thanks
private GoogleMap gMap;
MapFragment googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_search);
googleMap= (MapFragment) getFragmentManager().findFragmentById(
R.id.googleMap);
gMap= googleMap.getMap();
In you xml you are using MapFragment.Change it -
<fragment
android:id="#+id/mapfragment"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
class="com.google.android.gms.maps.SupportMapFragment" />
java -
googleMap = ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.mapfragment)).getMap();
I'm trying to use google map in fragment, but I meet a problem I don't understand.
I don't arrive to load the map in this line :
gMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
This line seems to return always null...
I tried to use MapFragment inseatd of SupportMapFragment, but it's not possible.
How to resolve it ?
To explain why i want to show it in a fragment, it's because i'm using a navigationdrawer, and one of my item needs to show the map.
thx,
EDIT : My xml :
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
I think you should call with getsupportFragmentManager not with getFragmentManager otherwise it will return null always
If you are using SupportMapFragment, you need to change your layout!
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
This is a basic example using SupportMapFragment:
public class MainActivity extends ActionBarActivity implements OnMapReadyCallback{
private SupportMapFragment map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
map.getMapAsync(this);//remember getMap() is deprecated!
}
#Override
public void onMapReady(GoogleMap map) {
map.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(47.17, 27.5699), 16));
map.addMarker(new MarkerOptions() .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)).anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
.position(new LatLng(47.17, 27.5699))); //Iasi, Romania
map.setMyLocationEnabled(true);
}
}
and change the reference in your layout:
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
I dont think it is possible as google map contains a < fragment> tag. See here why:
Note: You cannot inflate a layout into a fragment when that layout includes a . Nested fragments are only supported when added to a fragment dynamically.
Some people in SO however managed to show map in a fragment, but the solution was hacky.
Google map doesn't show marker.
Here is my code snippet used for adding marker
googleMap = ((MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
return ;
}
try {
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(0, 0))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
} catch (Exception e) {
Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_LONG).show();
}
Here is my xml layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
Seems everything is fine with map and its showing in actual screen. Note that I am using this fragment inside the fragment (support lib) not extended from MapFragment.
Any ideas?
Change your LatLng and set zoom on map to that place and see if is showing there...
For example:
map.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(44.797283, 20.460663), 12))
map.addMarker(new MarkerOptions()
.position(new LatLng(44.797283, 20.460663))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
For more information: How to add a marker on GoogeMapAPIv2
Replace this line
googleMap = ((MapFragment)getActivity().getFragmentManager().findFragmentById(R.id.map))
.getMap();
By this
googleMap = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();