24 |
25 | Learn all features about Google Map API from Basic to Advance. from Creating a Google Map API key the easiest way possible to changing themes and styles and changing user's location source.
26 |
27 | There is also a Udemy course available for this repository.
28 | if you want to learn how to work with Google Maps API please check my course: [Complete Google Map API for Android Tutorial](https://www.udemy.com/course/the-complete-google-map-api-for-android-tutorial/?referralCode=07C6875E9237D69DA280)
29 |
30 |
31 | You will learn these topics for Google Map:
32 | * Getting API Key
33 | * Map Basics
34 | * Map Navigations Basics
35 | * Map Camera Clamp and tilt
36 | * Map Camera Events
37 | * Marker
38 | * Marker Listeners
39 | * Shapes
40 | * Shapes Listeners
41 | * Types and Styles
42 | * Map Settings
43 | * Map Snapshot
44 | * Getting User's Location
45 | * Changing User's Location Source
46 |
47 | You will learn these topics for StreetViewPanorama:
48 | * StreetView Basics
49 | * StreetView Navigations
50 | * StreetView Events
51 | * StreetView Settings
52 | * StreetView and Map
53 |
54 |
55 | ## Requirement
56 | Get Google Map API Key from here: https://console.developers.google.com/apis/credentials
57 |
58 | ## Checkout My Libraries
59 | * **[Android-Intent-Library](https://github.com/mohammadima3oud/Android-Intent-Library):** A library which will save you a lot of time from writing the same intent creation code. it consist of many intent creation codes like Share, Contacts, Email and etc, which you can easily use.
60 | * **[Material-Resources-Library](https://github.com/mohammadima3oud/Material-Resources-Library):** A list of most useful resources for designing android apps such as all material colors and dimens, 180 Gradient background + html, social, flat, fluent, metro colors.
61 | * **[Complete-Google-Map-API-Tutorial](https://github.com/mohammadima3oud/Complete-Google-Map-API-Tutorial):** Learn How to use Google Map API for Android from Basic to Advance with complete examples.
62 | * **[DropSignIn](https://github.com/mohammadima3oud/DropSignIn):** Sign In UI Design
63 | * **[BlueSignIn](https://github.com/mohammadima3oud/BlueSignIn):** Sign In and Sign Up Ui Design
64 |
65 | ## Donations
66 | This project needs you! If you would like to support this project's further development, the creator of this project or the continuous maintenance of this project, feel free to donate. Your donation is highly appreciated. Thank you!
67 |
68 |
69 | * **[Donate $5](https://www.paypal.me/mohammadima3oud/5)**: Thank's for creating this project, here's a tea (or some juice) for you!
70 | * **[Donate $10](https://www.paypal.me/mohammadima3oud/10)**: Wow, I am stunned. Let me take you to the movies!
71 | * **[Donate $15](https://www.paypal.me/mohammadima3oud/15)**: I really appreciate your work, let's grab some lunch!
72 | * **[Donate $25](https://www.paypal.me/mohammadima3oud/25)**: That's some awesome stuff you did right there, dinner is on me!
73 | * **[Donate $50](https://www.paypal.me/mohammadima3oud/50)**: I really really want to support this project, great job!
74 | * **[Donate $100](https://www.paypal.me/mohammadima3oud/100)**: You are the man! This project saved me hours (if not days) of struggle and hard work, simply awesome!
75 | * **[Donate $2799](https://www.paypal.me/mohammadima3oud/2799)**: Go buddy, buy Macbook Pro for yourself!
76 |
77 | Of course, you can also choose what you want to donate, all donations are awesome!
78 |
79 |
80 | ## Changelog
81 | * **1.0.0**
82 | * Initial release
83 |
84 |
85 | ## License
86 |
87 | Copyright 2019 mohammadima3oud
88 |
89 | Licensed under the Apache License, Version 2.0 (the "License");
90 | you may not use this file except in compliance with the License.
91 | You may obtain a copy of the License at
92 |
93 | http://www.apache.org/licenses/LICENSE-2.0
94 |
95 | Unless required by applicable law or agreed to in writing, software
96 | distributed under the License is distributed on an "AS IS" BASIS,
97 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
98 | See the License for the specific language governing permissions and
99 | limitations under the License.
100 |
--------------------------------------------------------------------------------
/app/src/main/java/com/next/googlemapapi/PermissionUtils.java:
--------------------------------------------------------------------------------
1 | package com.next.googlemapapi;
2 |
3 | import android.Manifest;
4 | import android.app.AlertDialog;
5 | import android.app.Dialog;
6 | import android.content.DialogInterface;
7 | import android.content.pm.PackageManager;
8 | import android.os.Bundle;
9 | import android.widget.Toast;
10 |
11 | import androidx.core.app.ActivityCompat;
12 | import androidx.fragment.app.DialogFragment;
13 | import androidx.fragment.app.FragmentActivity;
14 |
15 | public abstract class PermissionUtils
16 | {
17 | public static void requestPermission(FragmentActivity activity, int requestId, String permission, boolean finishActivity)
18 | {
19 | if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission))
20 | {
21 | // Display a dialog with rationale.
22 | PermissionUtils.RationaleDialog.newInstance(requestId, finishActivity).show(activity.getSupportFragmentManager(), "dialog");
23 | } else
24 | {
25 | // Location permission has not been granted yet, request it.
26 | ActivityCompat.requestPermissions(activity, new String[]{permission}, requestId);
27 | }
28 | }
29 |
30 | public static boolean isPermissionGranted(String[] grantPermissions, int[] grantResults, String permission)
31 | {
32 | for (int i = 0; i < grantPermissions.length; i++)
33 | {
34 | if (permission.equals(grantPermissions[i]))
35 | {
36 | return grantResults[i] == PackageManager.PERMISSION_GRANTED;
37 | }
38 | }
39 | return false;
40 | }
41 |
42 | public static class PermissionDeniedDialog extends DialogFragment
43 | {
44 | private static final String ARGUMENT_FINISH_ACTIVITY = "finish";
45 |
46 | private boolean mFinishActivity = false;
47 |
48 | public static PermissionDeniedDialog newInstance(boolean finishActivity)
49 | {
50 | Bundle arguments = new Bundle();
51 | arguments.putBoolean(ARGUMENT_FINISH_ACTIVITY, finishActivity);
52 |
53 | PermissionDeniedDialog dialog = new PermissionDeniedDialog();
54 | dialog.setArguments(arguments);
55 | return dialog;
56 | }
57 |
58 | @Override
59 | public Dialog onCreateDialog(Bundle savedInstanceState)
60 | {
61 | mFinishActivity = getArguments().getBoolean(ARGUMENT_FINISH_ACTIVITY);
62 |
63 | return new AlertDialog.Builder(getActivity())
64 | .setMessage("This sample requires location permission to enable the \\'my location\\' layer. Please try again and grant access to use the location.\\nIf the permission has been permanently denied, it can be enabled from the System Settings > Apps > \\'Google Maps API Demos\\'.")
65 | .setPositiveButton(android.R.string.ok, null)
66 | .create();
67 | }
68 |
69 | @Override
70 | public void onDismiss(DialogInterface dialog)
71 | {
72 | super.onDismiss(dialog);
73 | if (mFinishActivity)
74 | {
75 | Toast.makeText(getActivity(), "Location permission is required for this demo.", Toast.LENGTH_SHORT).show();
76 | getActivity().finish();
77 | }
78 | }
79 | }
80 |
81 | public static class RationaleDialog extends DialogFragment
82 | {
83 | private static final String ARGUMENT_PERMISSION_REQUEST_CODE = "requestCode";
84 | private static final String ARGUMENT_FINISH_ACTIVITY = "finish";
85 | private boolean mFinishActivity = false;
86 |
87 | public static RationaleDialog newInstance(int requestCode, boolean finishActivity)
88 | {
89 | Bundle arguments = new Bundle();
90 | arguments.putInt(ARGUMENT_PERMISSION_REQUEST_CODE, requestCode);
91 | arguments.putBoolean(ARGUMENT_FINISH_ACTIVITY, finishActivity);
92 | RationaleDialog dialog = new RationaleDialog();
93 | dialog.setArguments(arguments);
94 | return dialog;
95 | }
96 |
97 | @Override
98 | public Dialog onCreateDialog(Bundle savedInstanceState)
99 | {
100 | Bundle arguments = getArguments();
101 | final int requestCode = arguments.getInt(ARGUMENT_PERMISSION_REQUEST_CODE);
102 | mFinishActivity = arguments.getBoolean(ARGUMENT_FINISH_ACTIVITY);
103 |
104 | return new AlertDialog.Builder(getActivity())
105 | .setMessage("Access to the location service is required to demonstrate the \\'my location\\' feature, which shows your current location on the map.")
106 | .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
107 | {
108 | @Override
109 | public void onClick(DialogInterface dialog, int which)
110 | {
111 | // After click on Ok, request the permission.
112 | ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, requestCode);
113 | // Do not finish the Activity while requesting permission.
114 | mFinishActivity = false;
115 | }
116 | })
117 | .setNegativeButton(android.R.string.cancel, null)
118 | .create();
119 | }
120 |
121 | @Override
122 | public void onDismiss(DialogInterface dialog)
123 | {
124 | super.onDismiss(dialog);
125 | if (mFinishActivity)
126 | {
127 | Toast.makeText(getActivity(), "Location permission is required for this demo.", Toast.LENGTH_SHORT).show();
128 | getActivity().finish();
129 | }
130 | }
131 | }
132 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_map_navigation.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
15 |
16 |
22 |
23 |
29 |
30 |
36 |
37 |
43 |
44 |
50 |
51 |
57 |
58 |
64 |
65 |
72 |
73 |
80 |
81 |
90 |
91 |
100 |
101 |
110 |
111 |
120 |
121 |
130 |
131 |
138 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/java/com/next/googlemapapi/map/MarkerActivity.java:
--------------------------------------------------------------------------------
1 | package com.next.googlemapapi.map;
2 |
3 | import android.graphics.Bitmap;
4 | import android.graphics.Canvas;
5 | import android.graphics.drawable.Drawable;
6 | import android.os.Bundle;
7 | import android.util.Log;
8 | import android.view.View;
9 | import android.widget.Button;
10 |
11 | import androidx.core.content.ContextCompat;
12 | import androidx.fragment.app.FragmentActivity;
13 |
14 | import com.google.android.gms.maps.GoogleMap;
15 | import com.google.android.gms.maps.OnMapReadyCallback;
16 | import com.google.android.gms.maps.SupportMapFragment;
17 | import com.google.android.gms.maps.model.BitmapDescriptor;
18 | import com.google.android.gms.maps.model.BitmapDescriptorFactory;
19 | import com.google.android.gms.maps.model.LatLng;
20 | import com.google.android.gms.maps.model.Marker;
21 | import com.google.android.gms.maps.model.MarkerOptions;
22 | import com.next.googlemapapi.CustomInfoWindowAdapter;
23 | import com.next.googlemapapi.MainActivity;
24 | import com.next.googlemapapi.R;
25 |
26 | public class MarkerActivity extends FragmentActivity implements OnMapReadyCallback, View.OnClickListener
27 | , GoogleMap.OnMarkerClickListener, GoogleMap.OnInfoWindowClickListener, GoogleMap.OnMarkerDragListener,
28 | GoogleMap.OnInfoWindowCloseListener, GoogleMap.OnInfoWindowLongClickListener
29 | {
30 | private GoogleMap googleMap;
31 | private static final LatLng SYDNEY = new LatLng(-34, 151);
32 | private static final LatLng BRISBANE = new LatLng(-27.47093, 153.0235);
33 | private static final LatLng ADELAIDE = new LatLng(-34.92873, 138.59995);
34 |
35 | @Override
36 | protected void onCreate(Bundle savedInstanceState)
37 | {
38 | super.onCreate(savedInstanceState);
39 | setContentView(R.layout.activity_marker);
40 |
41 | SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
42 | mapFragment.getMapAsync(this);
43 |
44 | initialize();
45 | }
46 |
47 | private void initialize()
48 | {
49 | Button bSimpleMarker = findViewById(R.id.bSimpleMarker);
50 | Button bMarkerOption = findViewById(R.id.bMarkerOption);
51 | Button bCustomMarker = findViewById(R.id.bCustomMarker);
52 | bSimpleMarker.setOnClickListener(this);
53 | bMarkerOption.setOnClickListener(this);
54 | bCustomMarker.setOnClickListener(this);
55 | }
56 |
57 | @Override
58 | public void onMapReady(GoogleMap map)
59 | {
60 | googleMap = map;
61 | googleMap.setOnMarkerClickListener(this);
62 | googleMap.setOnInfoWindowClickListener(this);
63 | googleMap.setOnMarkerDragListener(this);
64 | googleMap.setOnInfoWindowCloseListener(this);
65 | googleMap.setOnInfoWindowLongClickListener(this);
66 | }
67 |
68 | @Override
69 | public void onClick(View view)
70 | {
71 | switch (view.getId())
72 | {
73 | case R.id.bSimpleMarker:
74 | googleMap.clear();
75 | googleMap.addMarker(new MarkerOptions().position(SYDNEY).title("Sydney"));
76 | break;
77 | case R.id.bMarkerOption:
78 | googleMap.clear();
79 | CustomInfoWindowAdapter.useDefaultInfoWindow=true;
80 | addMarkersToMap();
81 | googleMap.setInfoWindowAdapter(new CustomInfoWindowAdapter(MarkerActivity.this));
82 | break;
83 | case R.id.bCustomMarker:
84 | googleMap.clear();
85 | CustomInfoWindowAdapter.useDefaultInfoWindow=false;
86 | addMarkersToMap();
87 | googleMap.setInfoWindowAdapter(new CustomInfoWindowAdapter(MarkerActivity.this));
88 | break;
89 | }
90 | }
91 |
92 | private void addMarkersToMap()
93 | {
94 | MarkerOptions options =new MarkerOptions();
95 | options.position(BRISBANE);
96 | options.title("brisbane");
97 | options.snippet("Population: 2,544,634");
98 | options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
99 | options.draggable(true);
100 | CustomInfoWindowAdapter.brisbane=googleMap.addMarker(options);
101 |
102 |
103 | MarkerOptions options2 =new MarkerOptions();
104 | options2.position(ADELAIDE);
105 | options2.title("adelaide");
106 | options2.snippet("Population: 3,543,222");
107 | Drawable drawable=ContextCompat.getDrawable(getApplicationContext(),R.drawable.ic_person_pin_circle_black_24dp);
108 | options2.icon(convertDrawableToBitmap(drawable));
109 | options2.draggable(true);
110 | CustomInfoWindowAdapter.adelaide=googleMap.addMarker(options2);
111 | }
112 |
113 | private BitmapDescriptor convertDrawableToBitmap(Drawable drawable)
114 | {
115 | Canvas canvas = new Canvas();
116 | Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
117 | canvas.setBitmap(bitmap);
118 | drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
119 | drawable.draw(canvas);
120 | return BitmapDescriptorFactory.fromBitmap(bitmap);
121 | }
122 |
123 | @Override
124 | public boolean onMarkerClick(Marker marker)
125 | {
126 | Log.d(MainActivity.TAG, "onMarkerClick() called with: marker = [" + marker.getTitle() + "]");
127 | return false;
128 | }
129 |
130 | @Override
131 | public void onInfoWindowClick(Marker marker)
132 | {
133 | Log.d(MainActivity.TAG, "onInfoWindowClick() called with: marker = [" + marker.getTitle() + "]");
134 | }
135 |
136 | @Override
137 | public void onMarkerDragStart(Marker marker)
138 | {
139 | Log.d(MainActivity.TAG, "onMarkerDragStart() called with: marker = [" + marker.getTitle() + "]");
140 | }
141 |
142 | @Override
143 | public void onMarkerDrag(Marker marker)
144 | {
145 | Log.d(MainActivity.TAG, "onMarkerDrag() called with: marker = [" + marker.getTitle() + "]");
146 | }
147 |
148 | @Override
149 | public void onMarkerDragEnd(Marker marker)
150 | {
151 | Log.d(MainActivity.TAG, "onMarkerDragEnd() called with: marker = [" + marker.getTitle() + "]");
152 | }
153 |
154 | @Override
155 | public void onInfoWindowClose(Marker marker)
156 | {
157 | Log.d(MainActivity.TAG, "onInfoWindowClose() called with: marker = [" + marker.getTitle() + "]");
158 | }
159 |
160 | @Override
161 | public void onInfoWindowLongClick(Marker marker)
162 | {
163 | Log.d(MainActivity.TAG, "onInfoWindowLongClick() called with: marker = [" + marker.getTitle() + "]");
164 | }
165 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/next/googlemapapi/map/MapNavigationActivity.java:
--------------------------------------------------------------------------------
1 | package com.next.googlemapapi.map;
2 |
3 | import androidx.fragment.app.FragmentActivity;
4 |
5 | import android.os.Bundle;
6 | import android.view.View;
7 | import android.widget.Button;
8 |
9 | import com.google.android.gms.maps.CameraUpdateFactory;
10 | import com.google.android.gms.maps.GoogleMap;
11 | import com.google.android.gms.maps.OnMapReadyCallback;
12 | import com.google.android.gms.maps.SupportMapFragment;
13 | import com.google.android.gms.maps.model.CameraPosition;
14 | import com.google.android.gms.maps.model.LatLng;
15 | import com.google.android.gms.maps.model.LatLngBounds;
16 | import com.next.googlemapapi.R;
17 |
18 | public class MapNavigationActivity extends FragmentActivity implements OnMapReadyCallback, View.OnClickListener
19 | {
20 | private static final float SCROLL_BY_PX = 100;
21 | private GoogleMap googleMap;
22 | public static final CameraPosition BONDI = new CameraPosition.Builder()
23 | .target(new LatLng(-33.891614, 151.276417)).zoom(15.5f).bearing(300).tilt(25).build();
24 |
25 | public static final CameraPosition SYDNEY = new CameraPosition.Builder()
26 | .target(new LatLng(-33.87365, 151.20689)).zoom(15.5f).bearing(0).tilt(25).build();
27 |
28 | private static final CameraPosition ADELAIDE = new CameraPosition.Builder()
29 | .target(new LatLng(-34.92873, 138.59995)).zoom(12.0f).bearing(0).tilt(0).build();
30 |
31 | private static final CameraPosition KAWAKAMI = new CameraPosition.Builder()
32 | .target(new LatLng(35.92873, 138.59995)).zoom(12.0f).bearing(0).tilt(0).build();
33 |
34 | private static final LatLngBounds ADELAIDE_BOUNDS =
35 | new LatLngBounds(new LatLng(-35.0, 138.58), new LatLng(-34.9, 138.61));
36 |
37 | private static final LatLngBounds KAWAKAMI_BOUNDS =
38 | new LatLngBounds(new LatLng(35.9, 138.58), new LatLng(36.0, 138.61));
39 |
40 | @Override
41 | protected void onCreate(Bundle savedInstanceState)
42 | {
43 | super.onCreate(savedInstanceState);
44 | setContentView(R.layout.activity_map_navigation);
45 |
46 | SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
47 | mapFragment.getMapAsync(this);
48 |
49 | initialize();
50 | }
51 |
52 | private void initialize()
53 | {
54 | Button bTiltMore = findViewById(R.id.bTiltMore);
55 | Button bTiltLess = findViewById(R.id.bTiltLess);
56 | Button bZoomIn = findViewById(R.id.bZoomIn);
57 | Button bZoomOut = findViewById(R.id.bZoomOut);
58 | Button bPlayAnimation = findViewById(R.id.bPlayAnimation);
59 | Button bStopAnimation = findViewById(R.id.bStopAnimation);
60 | Button bScrollUp = findViewById(R.id.bScrollUp);
61 | Button bScrollLeft = findViewById(R.id.bScrollLeft);
62 | Button bScrollDown = findViewById(R.id.bScrollDown);
63 | Button bScrollRight = findViewById(R.id.bScrollRight);
64 | Button bGoToSydney = findViewById(R.id.bGoToSydney);
65 | Button bGoToBondi = findViewById(R.id.bGoToBondi);
66 | Button bGoToAdelaide = findViewById(R.id.bGoToAdelaide);
67 | Button bClampToKawakami = findViewById(R.id.bClampToKawakami);
68 | Button bClampToAdelaide = findViewById(R.id.bClampToAdelaide);
69 |
70 | bTiltMore.setOnClickListener(this);
71 | bTiltLess.setOnClickListener(this);
72 | bZoomIn.setOnClickListener(this);
73 | bZoomOut.setOnClickListener(this);
74 | bPlayAnimation.setOnClickListener(this);
75 | bStopAnimation.setOnClickListener(this);
76 | bScrollUp.setOnClickListener(this);
77 | bScrollLeft.setOnClickListener(this);
78 | bScrollDown.setOnClickListener(this);
79 | bScrollRight.setOnClickListener(this);
80 | bGoToSydney.setOnClickListener(this);
81 | bGoToBondi.setOnClickListener(this);
82 | bGoToAdelaide.setOnClickListener(this);
83 | bClampToKawakami.setOnClickListener(this);
84 | bClampToAdelaide.setOnClickListener(this);
85 | }
86 |
87 | @Override
88 | public void onMapReady(GoogleMap map)
89 | {
90 | googleMap = map;
91 | }
92 |
93 | @Override
94 | public void onClick(View view)
95 | {
96 | switch (view.getId())
97 | {
98 | case R.id.bTiltMore:
99 | tiltMore();
100 | break;
101 | case R.id.bTiltLess:
102 | tiltLess();
103 | break;
104 | case R.id.bZoomIn:
105 | googleMap.moveCamera(CameraUpdateFactory.zoomIn());
106 | break;
107 | case R.id.bZoomOut:
108 | googleMap.moveCamera(CameraUpdateFactory.zoomOut());
109 | break;
110 | case R.id.bScrollUp:
111 | googleMap.moveCamera(CameraUpdateFactory.scrollBy(0, -SCROLL_BY_PX));
112 | break;
113 | case R.id.bScrollLeft:
114 | googleMap.moveCamera(CameraUpdateFactory.scrollBy(-SCROLL_BY_PX, 0));
115 | break;
116 | case R.id.bScrollDown:
117 | googleMap.moveCamera(CameraUpdateFactory.scrollBy(0, SCROLL_BY_PX));
118 | break;
119 | case R.id.bScrollRight:
120 | googleMap.moveCamera(CameraUpdateFactory.scrollBy(SCROLL_BY_PX, 0));
121 | break;
122 | case R.id.bPlayAnimation:
123 | googleMap.setLatLngBoundsForCameraTarget(null);
124 | googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(SYDNEY));
125 | break;
126 | case R.id.bStopAnimation:
127 | googleMap.stopAnimation();
128 | break;
129 | case R.id.bGoToSydney:
130 | googleMap.setLatLngBoundsForCameraTarget(null);
131 | googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(SYDNEY));
132 | break;
133 | case R.id.bGoToBondi:
134 | googleMap.setLatLngBoundsForCameraTarget(null);
135 | googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(BONDI));
136 | break;
137 | case R.id.bGoToAdelaide:
138 | googleMap.setLatLngBoundsForCameraTarget(null);
139 | googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(ADELAIDE));
140 | break;
141 | case R.id.bClampToKawakami:
142 | googleMap.setLatLngBoundsForCameraTarget(KAWAKAMI_BOUNDS);
143 | googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(KAWAKAMI));
144 | break;
145 | case R.id.bClampToAdelaide:
146 | googleMap.setLatLngBoundsForCameraTarget(ADELAIDE_BOUNDS);
147 | googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(ADELAIDE));
148 | break;
149 | }
150 | }
151 |
152 | private void tiltMore()
153 | {
154 | CameraPosition currentCameraPosition = googleMap.getCameraPosition();
155 | float currentTilt = currentCameraPosition.tilt;
156 | float newTilt = currentTilt + 10;
157 | newTilt = (newTilt > 90) ? 90 : newTilt;
158 | CameraPosition cameraPosition = new CameraPosition.Builder(currentCameraPosition).tilt(newTilt).build();
159 | googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
160 | }
161 |
162 | private void tiltLess()
163 | {
164 | CameraPosition currentCameraPosition = googleMap.getCameraPosition();
165 | float currentTilt = currentCameraPosition.tilt;
166 | float newTilt = currentTilt - 10;
167 | newTilt = (newTilt > 0) ? newTilt : 0;
168 | CameraPosition cameraPosition = new CameraPosition.Builder(currentCameraPosition).tilt(newTilt).build();
169 | googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
170 | }
171 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/next/googlemapapi/map/ShapeActivity.java:
--------------------------------------------------------------------------------
1 | package com.next.googlemapapi.map;
2 |
3 | import androidx.fragment.app.FragmentActivity;
4 |
5 | import android.graphics.Color;
6 | import android.os.Bundle;
7 | import android.view.View;
8 | import android.widget.Button;
9 | import android.widget.Toast;
10 |
11 | import com.google.android.gms.maps.CameraUpdateFactory;
12 | import com.google.android.gms.maps.GoogleMap;
13 | import com.google.android.gms.maps.OnMapReadyCallback;
14 | import com.google.android.gms.maps.StreetViewPanorama;
15 | import com.google.android.gms.maps.SupportMapFragment;
16 | import com.google.android.gms.maps.model.BitmapDescriptorFactory;
17 | import com.google.android.gms.maps.model.Circle;
18 | import com.google.android.gms.maps.model.CircleOptions;
19 | import com.google.android.gms.maps.model.Dash;
20 | import com.google.android.gms.maps.model.Dot;
21 | import com.google.android.gms.maps.model.Gap;
22 | import com.google.android.gms.maps.model.GroundOverlay;
23 | import com.google.android.gms.maps.model.GroundOverlayOptions;
24 | import com.google.android.gms.maps.model.LatLng;
25 | import com.google.android.gms.maps.model.PatternItem;
26 | import com.google.android.gms.maps.model.Polygon;
27 | import com.google.android.gms.maps.model.PolygonOptions;
28 | import com.google.android.gms.maps.model.Polyline;
29 | import com.google.android.gms.maps.model.PolylineOptions;
30 | import com.next.googlemapapi.R;
31 |
32 | import java.util.Arrays;
33 | import java.util.List;
34 |
35 | public class ShapeActivity extends FragmentActivity implements OnMapReadyCallback, View.OnClickListener, GoogleMap.OnCircleClickListener, GoogleMap.OnPolygonClickListener, GoogleMap.OnPolylineClickListener, GoogleMap.OnGroundOverlayClickListener
36 | {
37 | private GoogleMap googleMap;
38 | private static final int PATTERN_DASH_LENGTH_PX = 100;
39 | private static final int PATTERN_GAP_LENGTH_PX = 200;
40 | private static final Dot DOT = new Dot();
41 | private static final Dash DASH = new Dash(PATTERN_DASH_LENGTH_PX);
42 | private static final Gap GAP = new Gap(PATTERN_GAP_LENGTH_PX);
43 | private static final List PATTERN_DOTTED = Arrays.asList(DOT, GAP);
44 | private static final List PATTERN_DASHED = Arrays.asList(DASH, GAP);
45 | private static final List PATTERN_MIXED = Arrays.asList(DOT, GAP, DOT, DASH, GAP);
46 |
47 | @Override
48 | protected void onCreate(Bundle savedInstanceState)
49 | {
50 | super.onCreate(savedInstanceState);
51 | setContentView(R.layout.activity_shape);
52 |
53 | SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
54 | mapFragment.getMapAsync(this);
55 |
56 | initialize();
57 | }
58 |
59 | private void initialize()
60 | {
61 | Button bCircle = findViewById(R.id.bCircle);
62 | Button bPolygone = findViewById(R.id.bPolygone);
63 | Button bPolyline = findViewById(R.id.bPolyline);
64 | Button bGroundOverlays = findViewById(R.id.bGroundOverlays);
65 | bCircle.setOnClickListener(this);
66 | bPolygone.setOnClickListener(this);
67 | bPolyline.setOnClickListener(this);
68 | bGroundOverlays.setOnClickListener(this);
69 | }
70 |
71 | @Override
72 | public void onMapReady(GoogleMap map)
73 | {
74 | googleMap = map;
75 | googleMap.setOnCircleClickListener(this);
76 | googleMap.setOnPolygonClickListener(this);
77 | googleMap.setOnPolylineClickListener(this);
78 | googleMap.setOnGroundOverlayClickListener(this);
79 | }
80 |
81 | @Override
82 | public void onClick(View view)
83 | {
84 | switch (view.getId())
85 | {
86 | case R.id.bCircle:
87 | addCircle();
88 | break;
89 | case R.id.bPolygone:
90 | addPolygon();
91 | break;
92 | case R.id.bPolyline:
93 | addPolyline();
94 | break;
95 | case R.id.bGroundOverlays:
96 | addGroundOverlays();
97 | break;
98 | }
99 | }
100 |
101 | private void addCircle()
102 | {
103 | googleMap.clear();
104 | CircleOptions circleOptions = new CircleOptions();
105 | circleOptions.center(new LatLng(37, 67));
106 | circleOptions.radius(100000);
107 | circleOptions.strokeColor(Color.RED);
108 | circleOptions.strokeWidth(10f);
109 | circleOptions.strokePattern(PATTERN_MIXED);
110 | circleOptions.clickable(true);
111 | circleOptions.fillColor(Color.GREEN);
112 |
113 | googleMap.addCircle(circleOptions).setTag(new CustomTag("circle"));
114 | googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(37, 67)));
115 | }
116 |
117 | private void addPolygon()
118 | {
119 | googleMap.clear();
120 | PolygonOptions polygonOptions = new PolygonOptions();
121 | polygonOptions.add(new LatLng(0, 0));
122 | polygonOptions.add(new LatLng(-3, 2.5));
123 | polygonOptions.add(new LatLng(0, 5));
124 | polygonOptions.add(new LatLng(3, 5));
125 | polygonOptions.add(new LatLng(3, 0));
126 | polygonOptions.add(new LatLng(0, 0));
127 | polygonOptions.clickable(true);
128 | polygonOptions.fillColor(Color.BLUE);
129 |
130 | googleMap.addPolygon(polygonOptions).setTag(new CustomTag("polygon"));
131 | googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(0, 0)));
132 | }
133 |
134 | private void addPolyline()
135 | {
136 | googleMap.clear();
137 | PolylineOptions polylineOptions = new PolylineOptions();
138 | polylineOptions.add(new LatLng(37.35, -122.0));
139 | polylineOptions.add(new LatLng(37.45, -122.0));
140 | polylineOptions.add(new LatLng(37.45, -122.2));
141 | polylineOptions.add(new LatLng(37.35, -122.2));
142 | polylineOptions.clickable(true);
143 | polylineOptions.color(Color.CYAN);
144 |
145 | googleMap.addPolyline(polylineOptions).setTag(new CustomTag("polyline"));
146 | googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(37.35, -122.0)));
147 | }
148 |
149 | private void addGroundOverlays()
150 | {
151 | googleMap.clear();
152 | GroundOverlayOptions groundOverlayOptions = new GroundOverlayOptions();
153 | groundOverlayOptions.image(BitmapDescriptorFactory.fromResource(R.drawable.city)).anchor(0, 1);
154 | groundOverlayOptions.position(new LatLng(40, -74), 8600f, 6500f);
155 | groundOverlayOptions.bearing(0);
156 | groundOverlayOptions.clickable(true);
157 |
158 | googleMap.addGroundOverlay(groundOverlayOptions).setTag(new CustomTag("ground overlay"));
159 | googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(40, -74)));
160 | }
161 |
162 | @Override
163 | public void onCircleClick(Circle circle)
164 | {
165 | onClick((CustomTag) circle.getTag());
166 | }
167 |
168 | @Override
169 | public void onPolygonClick(Polygon polygon)
170 | {
171 | onClick((CustomTag) polygon.getTag());
172 | }
173 |
174 | @Override
175 | public void onPolylineClick(Polyline polyline)
176 | {
177 | onClick((CustomTag) polyline.getTag());
178 | }
179 |
180 | @Override
181 | public void onGroundOverlayClick(GroundOverlay groundOverlay)
182 | {
183 | onClick((CustomTag) groundOverlay.getTag());
184 | }
185 |
186 | private void onClick(CustomTag tag)
187 | {
188 | tag.incrementClickCount();
189 | Toast.makeText(ShapeActivity.this, tag.toString(), Toast.LENGTH_SHORT).show();
190 | }
191 |
192 | private static class CustomTag
193 | {
194 | private final String description;
195 | private int clickCount;
196 |
197 | public CustomTag(String description)
198 | {
199 | this.description = description;
200 | this.clickCount = 0;
201 | }
202 |
203 | void incrementClickCount()
204 | {
205 | clickCount++;
206 | }
207 |
208 | @Override
209 | public String toString()
210 | {
211 | return "The " + description + " has been clicked " + clickCount + " Times.";
212 | }
213 | }
214 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/next/googlemapapi/streetview/StreetViewNavigationActivity.java:
--------------------------------------------------------------------------------
1 | package com.next.googlemapapi.streetview;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 | import android.widget.Button;
6 | import android.widget.Toast;
7 |
8 | import androidx.fragment.app.FragmentActivity;
9 |
10 | import com.google.android.gms.maps.OnStreetViewPanoramaReadyCallback;
11 | import com.google.android.gms.maps.StreetViewPanorama;
12 | import com.google.android.gms.maps.SupportStreetViewPanoramaFragment;
13 | import com.google.android.gms.maps.model.LatLng;
14 | import com.google.android.gms.maps.model.StreetViewPanoramaCamera;
15 | import com.google.android.gms.maps.model.StreetViewPanoramaLink;
16 | import com.google.android.gms.maps.model.StreetViewPanoramaLocation;
17 | import com.next.googlemapapi.R;
18 |
19 | public class StreetViewNavigationActivity extends FragmentActivity implements OnStreetViewPanoramaReadyCallback, View.OnClickListener
20 | {
21 | private static final LatLng SYDNEY = new LatLng(-33.87365, 151.20689);
22 | private static final LatLng SAN_FRANCISCO = new LatLng(37.769263, -122.450727);
23 | private static final int PAN_BY_DEG = 30;
24 | private static final float ZOOM_BY = 0.5f;
25 | private StreetViewPanorama streetViewPanorama;
26 |
27 | @Override
28 | protected void onCreate(Bundle savedInstanceState)
29 | {
30 | super.onCreate(savedInstanceState);
31 | setContentView(R.layout.activity_street_view_navigation);
32 |
33 | SupportStreetViewPanoramaFragment fragment = (SupportStreetViewPanoramaFragment) getSupportFragmentManager().findFragmentById(R.id.street_view);
34 | fragment.getStreetViewPanoramaAsync(this);
35 |
36 | initialize();
37 | }
38 |
39 | private void initialize()
40 | {
41 | Button bPanUp = findViewById(R.id.bPanUp);
42 | Button bPanDown = findViewById(R.id.bPanDown);
43 | Button bPanLeft = findViewById(R.id.bPanLeft);
44 | Button bPanRight = findViewById(R.id.bPanRight);
45 | Button bZoomIn = findViewById(R.id.bZoomIn);
46 | Button bZoomOut = findViewById(R.id.bZoomOut);
47 | Button bWalk = findViewById(R.id.bWalk);
48 | Button bPosition = findViewById(R.id.bPosition);
49 | Button bGoToSydney = findViewById(R.id.bGoToSydney);
50 | Button bGoToSanFrancisco = findViewById(R.id.bGoToSanFrancisco);
51 | bPanUp.setOnClickListener(this);
52 | bPanDown.setOnClickListener(this);
53 | bPanLeft.setOnClickListener(this);
54 | bPanRight.setOnClickListener(this);
55 | bZoomIn.setOnClickListener(this);
56 | bZoomOut.setOnClickListener(this);
57 | bWalk.setOnClickListener(this);
58 | bPosition.setOnClickListener(this);
59 | bGoToSydney.setOnClickListener(this);
60 | bGoToSanFrancisco.setOnClickListener(this);
61 | }
62 |
63 | @Override
64 | public void onStreetViewPanoramaReady(StreetViewPanorama streetView)
65 | {
66 | streetViewPanorama = streetView;
67 | streetViewPanorama.setPosition(SYDNEY);
68 | }
69 |
70 | @Override
71 | public void onClick(View view)
72 | {
73 | if (streetViewPanorama == null)
74 | {
75 | Toast.makeText(StreetViewNavigationActivity.this, "StreetView is not ready", Toast.LENGTH_SHORT).show();
76 | return;
77 | }
78 | switch (view.getId())
79 | {
80 | case R.id.bPanUp:
81 | panUp();
82 | break;
83 | case R.id.bPanDown:
84 | panDown();
85 | break;
86 | case R.id.bPanRight:
87 | panRight();
88 | break;
89 | case R.id.bPanLeft:
90 | panLeft();
91 | break;
92 | case R.id.bZoomIn:
93 | zoomIn();
94 | break;
95 | case R.id.bZoomOut:
96 | zoomOut();
97 | break;
98 | case R.id.bWalk:
99 | walk();
100 | break;
101 | case R.id.bPosition:
102 | Toast.makeText(view.getContext(), streetViewPanorama.getLocation().position.toString(), Toast.LENGTH_SHORT).show();
103 | break;
104 | case R.id.bGoToSydney:
105 | streetViewPanorama.setPosition(SYDNEY);
106 | break;
107 | case R.id.bGoToSanFrancisco:
108 | streetViewPanorama.setPosition(SAN_FRANCISCO);
109 | break;
110 | }
111 | }
112 |
113 | public void panUp()
114 | {
115 | float currentTilt = streetViewPanorama.getPanoramaCamera().tilt;
116 | float newTilt = currentTilt + PAN_BY_DEG;
117 | newTilt = (newTilt > 90) ? 90 : newTilt;
118 |
119 | streetViewPanorama.animateTo(new StreetViewPanoramaCamera.Builder()
120 | .zoom(streetViewPanorama.getPanoramaCamera().zoom)
121 | .tilt(newTilt)
122 | .bearing(streetViewPanorama.getPanoramaCamera().bearing)
123 | .build(), 1000);
124 | }
125 |
126 | public void panDown()
127 | {
128 | float currentTilt = streetViewPanorama.getPanoramaCamera().tilt;
129 | float newTilt = currentTilt - PAN_BY_DEG;
130 | newTilt = (newTilt < -90) ? -90 : newTilt;
131 |
132 | streetViewPanorama.animateTo(new StreetViewPanoramaCamera.Builder()
133 | .zoom(streetViewPanorama.getPanoramaCamera().zoom)
134 | .tilt(newTilt)
135 | .bearing(streetViewPanorama.getPanoramaCamera().bearing)
136 | .build(), 1000);
137 | }
138 |
139 | public void panRight()
140 | {
141 | streetViewPanorama.animateTo(new StreetViewPanoramaCamera.Builder()
142 | .zoom(streetViewPanorama.getPanoramaCamera().zoom)
143 | .tilt(streetViewPanorama.getPanoramaCamera().tilt)
144 | .bearing(streetViewPanorama.getPanoramaCamera().bearing + PAN_BY_DEG)
145 | .build(), 1000);
146 | }
147 |
148 | public void panLeft()
149 | {
150 | streetViewPanorama.animateTo(new StreetViewPanoramaCamera.Builder()
151 | .zoom(streetViewPanorama.getPanoramaCamera().zoom)
152 | .tilt(streetViewPanorama.getPanoramaCamera().tilt)
153 | .bearing(streetViewPanorama.getPanoramaCamera().bearing - PAN_BY_DEG)
154 | .build(), 1000);
155 | }
156 |
157 | public void zoomIn()
158 | {
159 | streetViewPanorama.animateTo(new StreetViewPanoramaCamera.Builder()
160 | .zoom(streetViewPanorama.getPanoramaCamera().zoom + ZOOM_BY)
161 | .tilt(streetViewPanorama.getPanoramaCamera().tilt)
162 | .bearing(streetViewPanorama.getPanoramaCamera().bearing)
163 | .build(), 1000);
164 | }
165 |
166 | public void zoomOut()
167 | {
168 | streetViewPanorama.animateTo(new StreetViewPanoramaCamera.Builder()
169 | .zoom(streetViewPanorama.getPanoramaCamera().zoom - ZOOM_BY)
170 | .tilt(streetViewPanorama.getPanoramaCamera().tilt)
171 | .bearing(streetViewPanorama.getPanoramaCamera().bearing)
172 | .build(), 1000);
173 | }
174 |
175 | public void walk()
176 | {
177 | StreetViewPanoramaLocation location = streetViewPanorama.getLocation();
178 | StreetViewPanoramaCamera camera = streetViewPanorama.getPanoramaCamera();
179 | if (location != null && location.links != null)
180 | {
181 | StreetViewPanoramaLink link = findClosestLinkToBearing(location.links, camera.bearing);
182 | streetViewPanorama.setPosition(link.panoId);
183 | }
184 | }
185 |
186 | public static StreetViewPanoramaLink findClosestLinkToBearing(StreetViewPanoramaLink[] links, float bearing)
187 | {
188 | float minBearingDiff = 360;
189 | StreetViewPanoramaLink closestLink = links[0];
190 | for (StreetViewPanoramaLink link : links)
191 | {
192 | if (minBearingDiff > findNormalizedDifference(bearing, link.bearing))
193 | {
194 | minBearingDiff = findNormalizedDifference(bearing, link.bearing);
195 | closestLink = link;
196 | }
197 | }
198 | return closestLink;
199 | }
200 |
201 | // Find the difference between angle a and b as a value between 0 and 180
202 | public static float findNormalizedDifference(float a, float b)
203 | {
204 | float diff = a - b;
205 | float normalizedDiff = diff - (float) (360 * Math.floor(diff / 360.0f));
206 | return (normalizedDiff < 180.0f) ? normalizedDiff : 360.0f - normalizedDiff;
207 | }
208 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------