here is my activity_main.xml
<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="cc15_66.echomap.Main" >
<linearlayout> android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment>
android:id="@+id/mapFragment"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
class="com.google.android.gms.maps.SupportMapFragment" />
<linearlayout> android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
here is my main class
package cc15_66.echomap;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import cc15_66.echomap.LatLngInterpolator.LinearFixed;
public class Main extends FragmentActivity {
// Google Map
private GoogleMap googleMap;
private HashMap markersHashMap;
private Iterator<entry> iter;
private CameraUpdate cu;
private CustomMarker customMarkerOne, customMarkerTwo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// Loading map
initilizeMap();
initializeUiSettings();
initializeMapLocationSettings();
initializeMapTraffic();
initializeMapType();
initializeMapViewSettings();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
// initilizeMap();
}
private void initilizeMap() {
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapFragment)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
}
(findViewById(R.id.mapFragment)).getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= 16) {
(findViewById(R.id.mapFragment)).getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
(findViewById(R.id.mapFragment)).getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
setCustomMarkerOnePosition();
setCustomMarkerTwoPosition();
}
});
}
void setCustomMarkerOnePosition() {
customMarkerOne = new CustomMarker("markerOne", 40.7102747, -73.9945297);
addMarker(customMarkerOne);
}
void setCustomMarkerTwoPosition() {
customMarkerTwo = new CustomMarker("markerTwo", 43.7297251, -74.0675716);
addMarker(customMarkerTwo);
}
public void startAnimation(View v) {
animateMarker(customMarkerOne, new LatLng(40.0675716, 40.7297251));
}
public void zoomToMarkers(View v) {
zoomAnimateLevelToFitMarkers(120);
}
public void animateBack(View v) {
animateMarker(customMarkerOne, new LatLng(32.0675716, 27.7297251));
}
public void initializeUiSettings() {
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(false);
googleMap.getUiSettings().setTiltGesturesEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
}
public void initializeMapLocationSettings() {
googleMap.setMyLocationEnabled(true);
}
public void initializeMapTraffic() {
googleMap.setTrafficEnabled(true);
}
public void initializeMapType() {
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
public void initializeMapViewSettings() {
googleMap.setIndoorEnabled(true);
googleMap.setBuildingsEnabled(false);
}
//this is method to help us set up a Marker that stores the Markers we want to plot on the map
public void setUpMarkersHashMap() {
if (markersHashMap == null) {
markersHashMap = new HashMap();
}
}
//this is method to help us add a Marker into the hashmap that stores the Markers
public void addMarkerToHashMap(CustomMarker customMarker, Marker marker) {
setUpMarkersHashMap();
markersHashMap.put(customMarker, marker);
}
//this is method to help us find a Marker that is stored into the hashmap
public Marker findMarker(CustomMarker customMarker) {
iter = markersHashMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry mEntry = (Map.Entry) iter.next();
CustomMarker key = (CustomMarker) mEntry.getKey();
if (customMarker.getCustomMarkerId().equals(key.getCustomMarkerId())) {
Marker value = (Marker) mEntry.getValue();
return value;
}
}
return null;
}
//this is method to help us add a Marker to the map
public void addMarker(CustomMarker customMarker) {
MarkerOptions markerOption = new MarkerOptions().position(
new LatLng(customMarker.getCustomMarkerLatitude(), customMarker.getCustomMarkerLongitude())).icon(BitmapDescriptorFactory.defaultMarker());
Marker newMark = googleMap.addMarker(markerOption);
addMarkerToHashMap(customMarker, newMark);
}
//this is method to help us remove a Marker
public void removeMarker(CustomMarker customMarker) {
if (markersHashMap != null) {
if (findMarker(customMarker) != null) {
findMarker(customMarker).remove();
markersHashMap.remove(customMarker);
}
}
}
//this is method to help us fit the Markers into specific bounds for camera position
public void zoomAnimateLevelToFitMarkers(int padding) {
LatLngBounds.Builder b = new LatLngBounds.Builder();
iter = markersHashMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry mEntry = (Map.Entry) iter.next();
CustomMarker key = (CustomMarker) mEntry.getKey();
LatLng ll = new LatLng(key.getCustomMarkerLatitude(), key.getCustomMarkerLongitude());
b.include(ll);
}
LatLngBounds bounds = b.build();
// Change the padding as per needed
cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
googleMap.animateCamera(cu);
}
//this is method to help us move a Marker.
public void moveMarker(CustomMarker customMarker, LatLng latlng) {
if (findMarker(customMarker) != null) {
findMarker(customMarker).setPosition(latlng);
customMarker.setCustomMarkerLatitude(latlng.latitude);
customMarker.setCustomMarkerLongitude(latlng.longitude);
}
}
//this is method to animate the Marker. There are flavours for all Android versions
public void animateMarker(CustomMarker customMarker, LatLng latlng) {
if (findMarker(customMarker) != null) {
LatLngInterpolator latlonInter = new LinearFixed();
latlonInter.interpolate(20,
new LatLng(customMarker.getCustomMarkerLatitude(), customMarker.getCustomMarkerLongitude()), latlng);
customMarker.setCustomMarkerLatitude(latlng.latitude);
customMarker.setCustomMarkerLongitude(latlng.longitude);
}
}
}
here is my helper class customMaker
package cc15_66.echomap;
/**
* Created by abduljuuko on 7/12/2015.
*/
public class CustomMarker {
private String id;
private Double latitude;
private Double longitude;
public CustomMarker(String id, Double latitude, Double longitude) {
this.id = id;
this.latitude = latitude;
this.longitude = longitude;
}
public CustomMarker() {
this.id = "";
this.latitude = 0.0;
this.longitude = 0.0;
}
public String getCustomMarkerId() {
return id;
}
public void setCustomMarkerId(String id) {
this.id = id;
}
public Double getCustomMarkerLatitude() {
return latitude;
}
public void setCustomMarkerLatitude(Double mLatitude) {
this.latitude = mLatitude;
}
public Double getCustomMarkerLongitude() {
return longitude;
}
public void setCustomMarkerLongitude(Double mLongitude) {
this.longitude = mLongitude;
}
}
here is another helper class LatLngInterpolator
package cc15_66.echomap;
import static java.lang.Math.asin;
import static java.lang.Math.atan2;
import static java.lang.Math.cos;
import static java.lang.Math.pow;
import static java.lang.Math.sin;
import static java.lang.Math.sqrt;
import static java.lang.Math.toDegrees;
import static java.lang.Math.toRadians;
import com.google.android.gms.maps.model.LatLng;
/**
* Created by abduljuuko on 7/12/2015.
*/
public interface LatLngInterpolator {
public LatLng interpolate(float fraction, LatLng a, LatLng b);
public class Linear implements LatLngInterpolator {
@Override
public LatLng interpolate(float fraction, LatLng a, LatLng b) {
double lat = (b.latitude - a.latitude) * fraction + a.latitude;
double lng = (b.longitude - a.longitude) * fraction + a.longitude;
return new LatLng(lat, lng);
}
}
public class LinearFixed implements LatLngInterpolator {
@Override
public LatLng interpolate(float fraction, LatLng a, LatLng b) {
double lat = (b.latitude - a.latitude) * fraction + a.latitude;
double lngDelta = b.longitude - a.longitude;
if (Math.abs(lngDelta) > 180) {
lngDelta -= Math.signum(lngDelta) * 360;
}
double lng = lngDelta * fraction + a.longitude;
return new LatLng(lat, lng);
}
}
public class Spherical implements LatLngInterpolator {
@Override
public LatLng interpolate(float fraction, LatLng from, LatLng to) {
double fromLat = toRadians(from.latitude);
double fromLng = toRadians(from.longitude);
double toLat = toRadians(to.latitude);
double toLng = toRadians(to.longitude);
double cosFromLat = cos(fromLat);
double cosToLat = cos(toLat);
double angle = computeAngleBetween(fromLat, fromLng, toLat, toLng);
double sinAngle = sin(angle);
if (sinAngle < 1E-6) {
return from;
}
double a = sin((1 - fraction) * angle) / sinAngle;
double b = sin(fraction * angle) / sinAngle;
double x = a * cosFromLat * cos(fromLng) + b * cosToLat * cos(toLng);
double y = a * cosFromLat * sin(fromLng) + b * cosToLat * sin(toLng);
double z = a * sin(fromLat) + b * sin(toLat);
double lat = atan2(z, sqrt(x * x + y * y));
double lng = atan2(y, x);
return new LatLng(toDegrees(lat), toDegrees(lng));
}
private double computeAngleBetween(double fromLat, double fromLng, double toLat, double toLng) {
double dLat = fromLat - toLat;
double dLng = fromLng - toLng;
return 2 * asin(sqrt(pow(sin(dLat / 2), 2) + cos(fromLat) * cos(toLat) * pow(sin(dLng / 2), 2)));
}
}
}
here is my manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
package="cc15_66.echomap" >
<uses-permission android:name="android.permission.INTERNET">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">
<application>
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAjV7Zfq5ybTDmBf9Y9rEu5v9zpoz_SEow" />
<activity>
android:name=".Main"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.LAUNCHER">