Last active
September 10, 2015 06:59
-
-
Save dineshr93/6684beb04407d72ead8d to your computer and use it in GitHub Desktop.
Check google play service is ok
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public boolean servicesOK() { | |
| int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); | |
| if (isAvailable == ConnectionResult.SUCCESS) { | |
| return true; | |
| } | |
| else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) { | |
| Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST); | |
| dialog.show(); | |
| } | |
| else { | |
| Toast.makeText(this, "Can't connect to Google Play services", Toast.LENGTH_SHORT).show(); | |
| } | |
| return false; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| layout | |
| java. | |
| public void geoLocate(View v) throws IOException { | |
| hideSoftKeyboard(v); | |
| EditText et = (EditText) findViewById(R.id.editText1); | |
| String location = et.getText().toString(); | |
| Geocoder gc = new Geocoder(this); | |
| List<Address> list = gc.getFromLocationName(location, 1); | |
| Address add = list.get(0); | |
| String locality = add.getLocality(); | |
| Toast.makeText(this, locality, Toast.LENGTH_LONG).show(); | |
| double lat = add.getLatitude(); | |
| double lng = add.getLongitude(); | |
| gotoLocation(lat, lng, DEFAULTZOOM); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.gmapsapp; | |
| import java.io.IOException; | |
| import java.util.List; | |
| import android.app.Dialog; | |
| import android.location.Address; | |
| import android.location.Geocoder; | |
| import android.os.Bundle; | |
| import android.support.v4.app.FragmentActivity; | |
| import android.view.Menu; | |
| import android.view.MenuItem; | |
| import android.view.View; | |
| import android.view.inputmethod.InputMethodManager; | |
| import android.widget.EditText; | |
| import android.widget.Toast; | |
| import com.google.android.gms.common.ConnectionResult; | |
| import com.google.android.gms.common.GooglePlayServicesUtil; | |
| 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.LatLng; | |
| public class MainActivity extends FragmentActivity { | |
| private static final int GPS_ERRORDIALOG_REQUEST = 9001; | |
| GoogleMap mMap; | |
| @SuppressWarnings("unused") | |
| private static final double SEATTLE_LAT = 47.60621, | |
| SEATTLE_LNG =-122.33207, | |
| SYDNEY_LAT = -33.867487, | |
| SYDNEY_LNG = 151.20699, | |
| NEWYORK_LAT = 40.714353, | |
| NEWYORK_LNG = -74.005973; | |
| private static final float DEFAULTZOOM = 15; | |
| private static final String LOGTAG = "Maps"; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| if (servicesOK()) { | |
| setContentView(R.layout.activity_map); | |
| if (initMap()) { | |
| Toast.makeText(this, "Ready to map!", Toast.LENGTH_SHORT).show(); | |
| } | |
| else { | |
| Toast.makeText(this, "Map not available!", Toast.LENGTH_SHORT).show(); | |
| } | |
| } | |
| else { | |
| setContentView(R.layout.activity_main); | |
| } | |
| } | |
| @Override | |
| public boolean onCreateOptionsMenu(Menu menu) { | |
| // Inflate the menu; this adds items to the action bar if it is present. | |
| getMenuInflater().inflate(R.menu.main, menu); | |
| return true; | |
| } | |
| public boolean servicesOK() { | |
| int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); | |
| if (isAvailable == ConnectionResult.SUCCESS) { | |
| return true; | |
| } | |
| else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) { | |
| Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST); | |
| dialog.show(); | |
| } | |
| else { | |
| Toast.makeText(this, "Can't connect to Google Play services", Toast.LENGTH_SHORT).show(); | |
| } | |
| return false; | |
| } | |
| private boolean initMap() { | |
| if (mMap == null) { | |
| SupportMapFragment mapFrag = | |
| (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); | |
| mMap = mapFrag.getMap(); | |
| } | |
| return (mMap != null); | |
| } | |
| private void gotoLocation(double lat, double lng) { | |
| LatLng ll = new LatLng(lat, lng); | |
| CameraUpdate update = CameraUpdateFactory.newLatLng(ll); | |
| mMap.moveCamera(update); | |
| } | |
| private void gotoLocation(double lat, double lng, | |
| float zoom) { | |
| LatLng ll = new LatLng(lat, lng); | |
| CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom); | |
| mMap.moveCamera(update); | |
| } | |
| public void geoLocate(View v) throws IOException { | |
| hideSoftKeyboard(v); | |
| EditText et = (EditText) findViewById(R.id.editText1); | |
| String location = et.getText().toString(); | |
| Geocoder gc = new Geocoder(this); | |
| List<Address> list = gc.getFromLocationName(location, 1); | |
| Address add = list.get(0); | |
| String locality = add.getLocality(); | |
| Toast.makeText(this, locality, Toast.LENGTH_LONG).show(); | |
| double lat = add.getLatitude(); | |
| double lng = add.getLongitude(); | |
| gotoLocation(lat, lng, DEFAULTZOOM); | |
| } | |
| private void hideSoftKeyboard(View v) { | |
| InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); | |
| imm.hideSoftInputFromWindow(v.getWindowToken(), 0); | |
| } | |
| @Override | |
| public boolean onOptionsItemSelected(MenuItem item) { | |
| switch (item.getItemId()) { | |
| case R.id.mapTypeNone: | |
| mMap.setMapType(GoogleMap.MAP_TYPE_NONE); | |
| break; | |
| case R.id.mapTypeNormal: | |
| mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); | |
| break; | |
| case R.id.mapTypeSatellite: | |
| mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); | |
| break; | |
| case R.id.mapTypeTerrain: | |
| mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN); | |
| break; | |
| case R.id.mapTypeHybrid: | |
| mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); | |
| break; | |
| default: | |
| break; | |
| } | |
| return super.onOptionsItemSelected(item); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <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" | |
| android:paddingBottom="@dimen/activity_vertical_margin" | |
| android:paddingLeft="@dimen/activity_horizontal_margin" | |
| android:paddingRight="@dimen/activity_horizontal_margin" | |
| android:paddingTop="@dimen/activity_vertical_margin" > | |
| <LinearLayout | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:orientation="horizontal" > | |
| <TextView | |
| android:id="@+id/textView1" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:layout_gravity="left|center_vertical" | |
| android:text="Location:" | |
| android:textSize="20sp" /> | |
| <EditText | |
| android:id="@+id/editText1" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:layout_gravity="center|center_vertical" | |
| android:ems="10" > | |
| <requestFocus /> | |
| </EditText> | |
| <Button | |
| android:id="@+id/button1" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:layout_gravity="right|center_vertical" | |
| android:text="Go" | |
| android:onClick="geoLocate"/> | |
| </LinearLayout> | |
| <fragment | |
| android:id="@+id/map" | |
| android:name="com.google.android.gms.maps.SupportMapFragment" | |
| android:layout_width="match_parent" | |
| android:layout_height="fill_parent" /> | |
| </LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private boolean initMap() { | |
| if (mMap == null) { | |
| SupportMapFragment mapFrag = | |
| (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); | |
| mMap = mapFrag.getMap(); | |
| } | |
| return (mMap != null); | |
| } | |
| In java | |
| if (servicesOK()) { | |
| setContentView(R.layout.activity_map); | |
| if (initMap()) { //check for instance and use | |
| Toast.makeText(this, "Ready to map!", Toast.LENGTH_SHORT).show(); | |
| } | |
| else { | |
| Toast.makeText(this, "Map not available!", Toast.LENGTH_SHORT).show(); | |
| } | |
| } | |
| else { | |
| setContentView(R.layout.activity_main); | |
| } | |
| In layout activity_map.xml | |
| <?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" | |
| android:id="@+id/map" | |
| android:name="com.google.android.gms.maps.SupportMapFragment" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| onDestroy() | |
| onPause() | |
| onResume() | |
| onSaveInstanceState() | |
| onLowMemory() | |
| In Layout | |
| <com.google.android.gms.maps.MapView | |
| android:id="@+id/map" | |
| android:layout_width="match_parent" | |
| android:layout_height="matcch_parent" /> | |
| In Java | |
| get reference to the mapview using MapView object. | |
| mapview = findviewById | |
| mapview.onCreate(savedInstanceState); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <menu xmlns:android="http://schemas.android.com/apk/res/android" > | |
| <item | |
| android:id="@+id/mapTypeNormal" | |
| android:title="Normal"/> | |
| <item | |
| android:id="@+id/mapTypeSatellite" | |
| android:title="Satellite"/> | |
| <item | |
| android:id="@+id/mapTypeTerrain" | |
| android:title="Terrain"/> | |
| <item | |
| android:id="@+id/mapTypeHybrid" | |
| android:title="Hybrid"/> | |
| <item | |
| android:id="@+id/mapTypeNone" | |
| android:title="None"/> | |
| </menu> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.gmapsapp; | |
| import com.google.android.gms.maps.GoogleMap; | |
| import com.google.android.gms.maps.model.CameraPosition; | |
| import com.google.android.gms.maps.model.LatLng; | |
| import android.content.Context; | |
| import android.content.SharedPreferences; | |
| public class MapStateManager { | |
| private static final String LONGITUDE = "longitude"; | |
| private static final String LATITUDE = "latitude"; | |
| private static final String ZOOM = "zoom"; | |
| private static final String BEARING = "bearing"; | |
| private static final String TILT = "tilt"; | |
| private static final String MAPTYPE = "MAPTYPE"; | |
| private static final String PREFS_NAME ="mapCameraState"; | |
| private SharedPreferences mapStatePrefs; | |
| public MapStateManager(Context context) { | |
| mapStatePrefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | |
| } | |
| public void saveMapState(GoogleMap map) { | |
| SharedPreferences.Editor editor = mapStatePrefs.edit(); | |
| CameraPosition position = map.getCameraPosition(); | |
| editor.putFloat(LATITUDE, (float) position.target.latitude); | |
| editor.putFloat(LONGITUDE, (float) position.target.longitude); | |
| editor.putFloat(ZOOM, position.zoom); | |
| editor.putFloat(TILT, position.tilt); | |
| editor.putFloat(BEARING, position.bearing); | |
| editor.putInt(MAPTYPE, map.getMapType()); | |
| editor.commit(); | |
| } | |
| public CameraPosition getSavedCameraPosition() { | |
| double latitude = mapStatePrefs.getFloat(LATITUDE, 0); | |
| if (latitude == 0) { | |
| return null; | |
| } | |
| double longitude = mapStatePrefs.getFloat(LONGITUDE, 0); | |
| LatLng target = new LatLng(latitude, longitude); | |
| float zoom = mapStatePrefs.getFloat(ZOOM, 0); | |
| float bearing = mapStatePrefs.getFloat(BEARING, 0); | |
| float tilt = mapStatePrefs.getFloat(TILT, 0); | |
| CameraPosition position = new CameraPosition(target, zoom, tilt, bearing); | |
| return position; | |
| } | |
| // This is part of the answer to the code challenge | |
| public int getSavedMapType() { | |
| return mapStatePrefs.getInt(MAPTYPE, GoogleMap.MAP_TYPE_NORMAL); | |
| } | |
| } | |
| in MainActivity.java | |
| @Override | |
| protected void onStop() { | |
| super.onStop(); | |
| MapStateManager mgr = new MapStateManager(this); | |
| mgr.saveMapState(mMap); | |
| } | |
| @Override | |
| protected void onResume() { | |
| super.onResume(); | |
| MapStateManager mgr = new MapStateManager(this); | |
| CameraPosition position = mgr.getSavedCameraPosition(); | |
| if (position != null) { | |
| CameraUpdate update = CameraUpdateFactory.newCameraPosition(position); | |
| mMap.moveCamera(update); | |
| // This is part of the answer to the code challenge | |
| mMap.setMapType(mgr.getSavedMapType()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private static final double SEATTLE_LAT = 47.60621, | |
| SEATTLE_LNG =-122.33207, | |
| SYDNEY_LAT = -33.867487, | |
| SYDNEY_LNG = 151.20699, | |
| NEWYORK_LAT = 40.714353, | |
| NEWYORK_LNG = -74.005973; | |
| private static final float DEFAULTZOOM = 15; | |
| Use below function to pass the values. | |
| private void gotoLocation(double lat, double lng, | |
| float zoom) { | |
| LatLng ll = new LatLng(lat, lng); | |
| CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom); | |
| mMap.moveCamera(update); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if (servicesOK()) { | |
| Toast.makeText(this, "Ready to map!", Toast.LENGTH_SHORT).show(); | |
| setContentView(R.layout.activity_map); | |
| } | |
| else { | |
| setContentView(R.layout.activity_main); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <fragment xmlns:android="http://schemas.android.com/apk/res/android" | |
| xmlns:map="http://schemas.android.com/apk/res-auto" | |
| android:id="@+id/map" | |
| android:name="com.google.android.gms.maps.SupportMapFragment" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" /> | |
| or | |
| <fragment | |
| android:id="@+id/map" | |
| android:name="com.google.android.gms.maps.SupportMapFragment" | |
| android:layout_width="match_parent" | |
| android:layout_height="fill_parent" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment