How to use Google Map's V2 MapView for Android to display the actionbar?
Define dependencies in your build.gradle
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.google.android.gms:play-services:7.8.0'
For getting title extend your activity from AppCompatActivity and set title with setTitle()
For using MapView you don't need to make any checks in onResume.
Just get your map by async method in onCreate.
This setUpMap
method will be called when map is ready.
public class MapActivity extends AppCompatActivity {
private GoogleMap mMap;
private MapView mapView;
private Marker marker;
private static final LatLng ONE = new LatLng(32.882216, -117.222028);
private static final LatLng TWO = new LatLng(32.872000, -117.232004);
private static final LatLng THREE = new LatLng(32.880252, -117.233034);
private static final LatLng FOUR = new LatLng(32.885200, -117.226003);
private ArrayList<LatLng> coords = new ArrayList<LatLng>();
private static final int POINTS = 4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapview);
setTitle("My Map");
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
setUpMap(googleMap);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
private void setUpMap(GoogleMap map) {
mMap = map;
coords.add(ONE);
coords.add(TWO);
coords.add(THREE);
coords.add(FOUR);
for (int i = 0; i < POINTS; i++) {
mMap.addMarker(new MarkerOptions()
.position(coords.get(i))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
}
}
}
How to put Google Maps V2 on a Fragment using ViewPager
By using this code we can setup MapView anywhere, inside any ViewPager or Fragment or Activity.
In the latest update of Google for Maps, only MapView is supported for fragments. MapFragment & SupportMapFragment didn't work for me.
Setting up the layout for showing the map in the file location_fragment.xml
:
<?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" >
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Now, we setup the Java class for showing the map in the file MapViewFragment.java
:
public class MapViewFragment extends Fragment {
MapView mMapView;
private GoogleMap googleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.location_fragment, container, false);
mMapView = (MapView) rootView.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume(); // needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
// For showing a move to my location button
googleMap.setMyLocationEnabled(true);
// For dropping a marker at a point on the Map
LatLng sydney = new LatLng(-34, 151);
googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker Title").snippet("Marker Description"));
// For zooming automatically to the location of the marker
CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
});
return rootView;
}
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
}
Finally you need to get the API Key for your app by registering your app at Google Cloud Console. Register your app as Native Android App.
In Version 2 Map view does not show map
Maybe the key isn't correct. You can try the following thing:
- Be sure you Enter your right Package name like this
- Test on a real device which updated latest google play. Or use emulator with this guide
Active google map api v2 for android at google console site
Related Topics
How Is Staticlayout Used in Android
How to Use Getsharedpreferences in Android
Version Conflict Updating to 8.4.0
Eclipse/Android:"Errors Running Builder 'Android Pre Compiler' on Project..."
Toolbar Overlapping Below Status Bar
Android Background Image Memory Usage
Actionbar Logo Centered and Action Items on Sides
Cardview Layout_Width="Match_Parent" Does Not Match Parent Recyclerview Width
Android - Extracting Cookies After Login in Webview
How to Display Image in Android's Textview
"Failure Delivering Result " - Onactivityforresult
How to Use Autocompletetextview and Populate It with Data from a Web API
How to Receive Serial Data Using Android Bluetooth
Android Volley + JSONobjectrequest Caching