Google Maps Display - android

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.

Related

Android google maps fragment issue

I am implementing Google maps in my android app. It works well but sometimes the app crashes and gives me this error: (Caused by: android.view.InflateException: Binary XML file line #2 in Error inflating class fragment).
I tried all solutions suggested but most of them are old and I still has the same issue.
Can you please help me with this issue while it still cause app crash.
this is the code used in Google Maps:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityTruckLocationBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
lat=lang="0";
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map2);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng myplace = new LatLng(29.2722744, 47.8585885);
//mMap.addMarker(new MarkerOptions().position(myplace).title("Marker in myplace"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myplace,11));
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(#NonNull LatLng latLng) {
lat=String.valueOf(latLng.latitude);
lang=String.valueOf(latLng.longitude);
redirect();
}
});
}
public void redirect(){
Intent intent=new Intent(TruckLocation.this,Truck_Profile.class);
intent.putExtra("intenttype","location");
intent.putExtra("lat",lat);
intent.putExtra("lang",lang);
startActivity(intent);
finish();
}
Here is the XML code:
<?xml version="1.0" encoding="utf-8"?>
<fragment 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:id="#+id/map2"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TruckLocation" />
You have an issue in TruckLocationActivity.xml , you can share this xml with us to help you .

Call MapActivity to MainActivity with markers android

I have a MapActivity (The default by Android) and a MainActivity. And i call the MapActivity with an include like this at the MainActivity:
<include
android:id="#+id/mapa"
layout="#layout/activity_maps"/>
It shows me the map but not the markers anyone knows why? In the MapActivity i have this:
private GoogleMap mMap;
#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;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
Thanks all!
Use this only dont call include layout it wont work properly
<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="com.example.mapwithmarker.MapsMarkerActivity" />

Maps API Fragment not showing Marker in DrawerLayout

I'm new with StackOverflow and Maps API. I'm developing an app where a Drawer Layout is being used. I want to use one of the Drawer fragments to show Maps and work with Markers.
private SupportMapFragment mapFragment;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mapFragment = (SupportMapFragment)
getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_maps, container, false);
mapFragment.getMapAsync(this);
return view;
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
this is the fragment_maps.xml
<FrameLayout 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="com.labcontrol.lca.view.MapsFragment">
<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="com.labcontrol.lca.controller.MainActivity" />
</FrameLayout>
In the official guide they made an example using an Activity, as you can see I tried to do it inside a fragment. is it possible?
The result is this: maps is working properly, but I can't see the Marker I created, and I spent too many hours trying to figure out why.
The only thing make me suspicious is this error when playing with Maps on the phone and changing the camera:
W/Google Maps Android API: GLHudOverlay deprecated; draw(): no-op
Found out it's a common issue these days, a very new bug. Somebody knows how to get it up and running? Is there something wrong with the code?
thanks a lot

Google Maps returns null

I have a Google Maps Activity. suddently it is shutting down because getMap() (java null pointer exeption) doesn't seem to work. I reinstalled some older apks that worked fine and but yet these are shutting down to. So it has to be something on the device or so, what can be the reason?
private GoogleMap googleMap;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
if(googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
Here is the xml file
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"/>
Logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.mareike.cityexplorer/de.mareike.cityexplorer.MainActivity}: java.lang.NullPointerException
Caused by: java.lang.NullPointerException
at com.google.l.a.ce.a(Unknown Source)
at com.google.maps.api.android.lib6.d.eg.<init>(Unknown Source)
at com.google.maps.api.android.lib6.d.ef.a(Unknown Source)
at com.google.maps.api.android.lib6.d.aq.<init>(Unknown Source)
at com.google.maps.api.android.lib6.d.ft.a(Unknown Source)
at com.google.android.gms.maps.internal.l.onTransact(SourceFile:167)
at android.os.Binder.transact(Binder.java:380)
at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.addMarker(Unknown Source)
at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
at de.mareike.cityexplorer.MainActivity.onCreate(MainActivity.java:88)
According to here, the getMap() method is deprecated and you need to use getMapAsync(OnMapReadyCallback) instead.
Sample code:
public class MapsActivity extends FragmentActivity 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 map) {
// Add a marker in Sydney, Australia, and move the camera.
LatLng sydney = new LatLng(-34, 151);
map.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 10));
}
}
and the activity_main.xml:
<fragment 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:id="#+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
For more details, please refer official doc here.
Check your layout activity_main should contains fragment having id map
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

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

Categories

Resources