Google maps api 2 doesnt show marker - android

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

Related

Google Maps Display

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.

To overlay two buttons on Google map such that each button click shows different set of markers on the map. Can this be done?

I need to add two buttons that overlays on the google map such that each click shows a different set of markers on the map. Is this achievable or do I need to change the approach?
My Fragment code:
public class HomeFragment extends Fragment {
public HomeFragment(){}
static final LatLng VELACHERY = new LatLng(12.9758, 80.2205);
static final LatLng TAMBARAM = new LatLng(12.9300, 80.1100);
private GoogleMap map;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
//creating Markers
Marker hamburg = map.addMarker(new MarkerOptions().position(VELACHERY)
.title("Velachery"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(TAMBARAM)
.title("Volunteer")
.snippet("I am safe")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(VELACHERY, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
//map.setOnMarkerClickListener(listener)
return rootView;
}
public void onDestroyView() {
super.onDestroyView();
if (map != null) {
getFragmentManager()
.beginTransaction()
.remove(getFragmentManager().findFragmentById(R.id.map))
.commit();
}
}
}
My Fragment layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
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>
Yes, it's possible.
You could use a MapView or MapFragment-SupportMapFragment in a FrameLayout with the layout that contains your two buttons
Use OnMapReadyCallback to get the Google Map instance getMap is deprecated - look for getMapAsync(OnMapReadyCallback)
Handle View.OnClickListener for buttons
Each time a button is pressed call GoogleMap.clear(); then add the markers you need to display for the button that is pressed
Layout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<Button
android:id="#+id/buttonA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button A"/>
<Button
android:id="#+id/buttonB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button B"/>
</LinearLayout>
</FrameLayout>
Code
buttonA = (Button)findViewById(R.id.buttonA);
buttonA.setOnClickListener(this);
buttonB = (Button)findViewById(R.id.buttonB);
buttonB.setOnClickListener(this);
mapView = (MapView)findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
Implement OnMapReadyCallback.onMapReady
#Override
public void onMapReady( GoogleMap googleMap )
{
this.googleMap = googleMap;
//Do any other setup you would like here
}

Load Google Map in Fragment / swipeable Tab

i am working with swipeable tabs and want to display a mapsfragment inside the second tab, but everytime i try to get access to the map, there appears a nullpointerexception. Following the code of my Fragment/Tab.
public class tab2_advanced extends Fragment implements OnMapReadyCallback{
private GoogleMap gmap;
private static View view;
MapView ourMap;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab2_erweitertezusage, container, false);
Log.i("TAB2", "Start.");
//without the following lines regarding null or not null, the fragment loads the map, but i want to set markers and select a position on the map.
if (gmap==null) {
Log.i("Tab2", "Map null");
gmap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
gmap.setMyLocationEnabled(true);
}
if (gmap != null) {
Log.i("Tab2", "Map not null");
gmap.setMyLocationEnabled(true);
}
return v;
}
#Override
public void onMapReady(GoogleMap googleMap) {
Log.i("Aufruf onMapReady", "Darstellung"+gmap);
//Später Übergabe der Pos aus DB
LatLng sidney= new LatLng(49.7685, 9.9382);
gmap.setMyLocationEnabled(true);
gmap.moveCamera(CameraUpdateFactory.newLatLngZoom(wuerzburg, 13));
gmap.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(wuerzburg));
}
My XML-File for this is the following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="50dp">
<LinearLayout
android:orientation="vertical"
android:id="#+id/layoutMap"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="200dp"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
Without the described part in comments in the code of the fragment, the map loads the content. There are still some parts missing in the case, if the map is not null, but i tried it and it lands everytime in null with a following NullpointerException.
Hoping for hints :)
Greetings
Okay, i solved the issue by:
if (gmap==null) {
Log.i("Tab2", "Map null");
gmap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
gmap.setMyLocationEnabled(true);
}
if (gmap != null) {
Log.i("Tab2", "Map not null");
gmap.setMyLocationEnabled(true);
gmap.getUiSettings().setZoomControlsEnabled(true);
actLocation.getLocation(getActivity(), locationResult);
LatLng actPos = new LatLng(organizerGPS.latitude, organizerGPS.longitude);
Log.i("tab2", "actPos: " + actPos);
gmap.setMyLocationEnabled(true);
gmap.moveCamera(CameraUpdateFactory.newLatLngZoom(actPos, 13));
...
The hint with the getChildFragmentManager was good, but the reason for the NullpointerException was, that the view was null and that results to the error. Now the initialisation of the map is right behind setting the view and in addition i added a test.

Camera Update Factory is Not Initiliaze in Google Map

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

Android fail to load google map in fragment

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.

Categories

Resources