I want to display multiple maps in my layout.
In my fragment I have a list view, in the list view item I have a mapView
<com.google.android.gms.maps.MapView
android:id="#+id/map_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
In the adapter file, in my get view method I do :
holder.mapView.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
LatLng sydney = new LatLng(-33.852, 151.211);
googleMap.addMarker(new MarkerOptions().position(sydney)
.title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
holder.mapView.onResume();
}
});
But nothing shows up.
I have created a google maps API key, and added the following to my manifest.
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="my api key, not restricted"/>
And I have added this dependency in the griddle file:
compile 'com.google.android.gms:play-services-maps:10.2.1'
Do you know why there is no map showing?
Go to :
file - new - google - Google Maps Activity and Understand the things.
Add this in your xml
tools:context="yourpackage.YourActivityName"
Related
I have recently started working on an android application that includes the GPS tracking of the user's position.
For that purpose I have gone through Google's guide to setting up the GoogleMaps API for Android and I have (to my knowledge) fully understood the steps it took me to get here including the correct API_KEY and the SHA1 Footprint for the debug version of google maps.
The included code sends a request for the map information to google's API and upon loading fully sets up the map and places a marker in Sydney.
But it doesn't place the marker. Or move the maps interface to Sydney. It just shows the map and appears to be centering itself around africas west-coast every time for some reason.
Here's how far i've gotten in the Java Activity code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pkw__route__content__gps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
try {
mapFragment.getMapAsync(this);
}
catch(NullPointerException npe){
npe.printStackTrace();
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney, Australia, and move the camera.
LatLng sydney = new LatLng(-34, 151);
current = mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
The fragment looks as follows:
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="257dp"
android:layout_height="224dp"
android:layout_marginStart="72dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="72dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:context=".MapsActivity" />
I have added the API key to the Manifest in a tag called "com.google.android.geo.API_KEY". I will not post it in here for obvious reasons.
As said, the Map initializes correctly but the Markers are not placed and the moveCamera call is also not executed correctly
EDIT:
I think this question needs some more backstory, I am using the Map inside a fragment which is integrated into another activity.
The reason why I think this is important is because I have tried to start the map activity through an intent in which case it will fully load up, place markers and work interactively as it should. Because all of this is in the onCreate() method. How do I do this without having to invoke the Activity and stop the other one?
The map is not loaded when onMapReady(GoogleMap googleMap) is called. You have to wait for the OnMapLoadedCallback Event.
You have set a OnMapLoadedCallback to the map in onMapReady(GoogleMap googleMap) and set the marker and zoom the map to the location in this callback.
I need to show world map on my application.
I tried to use MapView and set liteMode= true and zoomLevel=1 but its not working as i need. Also groundOverlay not supported in liteMode and i need to show multiple groundOverlay on WorldMap.
can anyone know about it?
Please refere below link i need to show full world map like this.
https://upload.wikimedia.org/wikipedia/commons/b/b0/World_location_map_%28equirectangular_180%29.svg
Below code i done :
<com.google.android.gms.maps.MapView
android:id="#id/mapView"
android:layout_width="match_parent"
android:layout_height="#dimen/dp_180"
map:liteMode="true"
map:cameraZoom="1"
map:mapType="normal"
/>
In my Java Class
#Override
public void onMapReady(GoogleMap googleMap) {
if (googleMap != null) {
this.googleMap = googleMap;
this.googleMap.getUiSettings().setCompassEnabled(false);
this.googleMap.getUiSettings().setAllGesturesEnabled(false);
this.googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
this.googleMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(getContext(), R.raw.style_json));
this.googleMap.getUiSettings().setMapToolbarEnabled(false);
this.googleMap.getUiSettings().setCompassEnabled(false);
this.googleMap.getUiSettings().setScrollGesturesEnabled(false);
}
In this is added zoomLevel=1 but in google map its not showing world map as like above image link i shared.
CameraPosition cameraPosition = new CameraPosition.Builder().
target(YourLocationPoint).
tilt(60).
zoom(15).
bearing(0).
build();
myMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
You can use tilt and bearing for achieving same Doc
Set style for Google map , in onMapReady of your activity.
googleMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(getActivity(), R.raw.customized_map_style));
you need to download the style. please go through below link
https://developers.google.com/maps/documentation/android-sdk/styling
update the styles as you wish.
I try to build a Chat Application in wich it should be possible for the user to send his location. I can pick the location via a PlcaPicker, this is all working fine. But when I try to display the Location in my MapView, it is empty without any Google Logo or Google Maps Symbol.
My Code for displaying the Location:
viewholder.mymap.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
LatLng coordinates = new LatLng(43.535027499999984, 11.17666015625001);
MapsInitializer.initialize(Chat_Room.this);
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.addMarker(new MarkerOptions().position(coordinates));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(coordinates,13));
viewholder.mymap.onResume();
}
});
And my MapView:
<com.google.android.gms.maps.MapView
android:id="#+id/mymap"
android:layout_width="250dp"
android:layout_height="200dp"
android:layout_alignParentEnd="true"/>
I read that I can also use a Fragment but I dont know how to implement it and why my code is not working.
EDIT
My Manifest
<manifest>
<application>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="my api key" />
</application>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
</manifest>
UPDATE
When I use a Fragment like this:
<fragment
android:id="#+id/mymap"
android:layout_width="250dp"
android:layout_height="200dp"
android:layout_alignParentEnd="true"
map:cameraZoom="13"
map:mapType="normal"
class="com.google.android.gms.maps.SupportMapFragment"
map:liteMode="true"/>
It shows the Google Logo on the left bottom corner, but I think my problem now is that my Adapter for my Recyclerview can't find the MapFragment, so it does not show anything. How can I find the MapFragment in my RecyclerAdapter?
I am using Google Nexus 4.2.2 - with Google API on Genymotion. The google maps fragment is coming up empty. I have registered with the API and providing the key in the manifest file. Thanks for your help.
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"
tools:context=".MapsTestActivity" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="0dp"/>
</RelativeLayout>
Activity:
package com.chaseit.activities;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import com.chaseit.R;
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 MapsTestActivity 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_maps_test);
// setupMap();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
setupMap();
super.onResume();
}
private void setupMap() {
GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
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);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.maps_test, menu);
return true;
}
}
I got the example code from http://www.vogella.com/articles/AndroidGoogleMaps/article.html
Inorder to use google maps v2 in our android application, one must use google-play-services library. This Library works only if context is passed to check whether services is available to that devices. Add this line in your oncreate
GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
Comment me about the result
Check the output log, search on maps, and see if warnings notices come up. In my case, I found:
09:03:34.197 E/Google Maps Android API( 7012): Authorization failure. Please see https://developers.google.com/maps/documentation/android-api/start for how to correctly set up the map.
03-22 09:03:34.198 E/Google Maps Android API( 7012): In the Google Developer Console (https://console.developers.google.com)
03-22 09:03:34.198 E/Google Maps Android API( 7012): Ensure that the "Google Maps Android API v2" is enabled.
03-22 09:03:34.198 E/Google Maps Android API( 7012): Ensure that the following Android Key exists:
Double check on the following:
That you gave the correct SHA1 fingerprint of your application in google developer console (For me, somehow this changed)
That you gave the correct package name
That you included they API key in the manifest file (Obvious, yes, I know)
That the proper permissions are also set in manifest
ACCESS_COARSE_LOCATION
ACCESS_FINE_LOCATION
ACCESS_LOCATION_EXTRA_COMMANDS
ACCESS_MOCK_LOCATION
If using an emulator, insure that Google Play Services (including Maps) is installed.
More detailed directions can be found here.
In my case , I did not find a solution,the map is blank when it is opened and navigating the map does nothing , but I had a PlaceAutocompleteFragment and when i use it to search for a place I move the map to that place and it works , the map is now shown and you can navigate it .
you don't have to use PlaceAutocompleteFragment you can just get around the problem by moving the camera to a place you want when the map loads and then navigate from there , this can be done by adding this to the onMapReady() function :
LatLng latLng = new LatLng(15.501501,32.4325101) ;// change to the value you want
float zoom = 15.0f;
mapfragment.getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));
I have added a map fragment to my app including a geographic marker and included my play services jar but I'm getting the following error on my marker code:
Error: Cannot cast from Fragment to SupportMapFragment
The snippet is as follows:
GoogleMap mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.addMarker(new MarkerOptions()
.position(new LatLng(53.271900177, -9.04889965057))
.title("GMI alway Location"));;
This is my xml declaration to reference it:
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" />
Anyone have any ideas as to where I'm going wrong? Thanks Brian J
You have to use the SupportFragmentManager and the FragmentActivity classes if you are using the SupportMapFragment in your layout.
So use the getSupportFragmentManager() method instead of getFragmentManager().