Java tutorial
/* * Copyright (C) 2012 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.happyhours.activity; import java.io.IOException; import java.util.List; import java.util.Locale; import com.app.happyhours.R; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks; import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener; import com.google.android.gms.location.LocationClient; import com.google.android.gms.location.LocationListener; import com.google.android.gms.location.LocationRequest; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener; import com.google.android.gms.maps.GoogleMap.OnMyLocationButtonClickListener; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.BitmapDescriptorFactory; import com.google.android.gms.maps.model.CameraPosition; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; import com.happyhours.util.NotificationUtils; import android.content.Context; import android.content.Intent; import android.content.pm.ActivityInfo; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.util.Log; import android.view.MenuItem; import android.view.View; import android.widget.TextView; import android.widget.Toast; /** * This demo shows how GMS Location can be used to check for changes to the users location. The * "My Location" button uses GMS Location to set the blue dot representing the users location. To * track changes to the users location on the map, we request updates from the * {@link LocationClient}. */ public class MyLocationDemoActivity extends FragmentActivity implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener, OnMarkerDragListener { public static final String LATITUDE = "latitude"; public static final String LONGITUDE = "LONGITUDE"; public static final String ADDRESS = "address"; private GoogleMap mMap; private LocationClient mLocationClient; private TextView mMessageView, address_text; private Context context; private String latitude = ""; private String longitude = ""; private String strAddress = ""; private boolean isMarkerPlaced = false; // These settings are the same as the settings for the map. They will in fact give you updates // at the maximal rates currently possible. private static final LocationRequest REQUEST = LocationRequest.create().setInterval(5000) // 5 seconds .setFastestInterval(16) // 16ms = 60fps .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_location_demo); mMessageView = (TextView) findViewById(R.id.message_text); address_text = (TextView) findViewById(R.id.address_text); context = this; getActionBar().setIcon(R.drawable.logo); getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setHomeButtonEnabled(true); getActionBar().setBackgroundDrawable(getResources().getDrawable(R.color.menu_text_color)); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } @Override protected void onResume() { super.onResume(); setUpMapIfNeeded(); setUpLocationClientIfNeeded(); mLocationClient.connect(); } @Override public void onPause() { super.onPause(); if (mLocationClient != null) { mLocationClient.disconnect(); } } private void setUpMapIfNeeded() { // Do a null check to confirm that we have not already instantiated the map. if (mMap == null) { // Try to obtain the map from the SupportMapFragment. mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); // Check if we were successful in obtaining the map. if (mMap != null) { mMap.setMyLocationEnabled(true); mMap.setOnMarkerDragListener(this); } } } private void setUpLocationClientIfNeeded() { if (mLocationClient == null) { mLocationClient = new LocationClient(getApplicationContext(), this, // ConnectionCallbacks this); // OnConnectionFailedListener } } /** * Implementation of {@link LocationListener}. */ @Override public void onLocationChanged(Location location) { if (!isMarkerPlaced) { mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())) .title("Melbourne").snippet("Population: 4,137,400") .icon(BitmapDescriptorFactory.fromResource(R.drawable.pegman)).draggable(true)); CameraPosition cameraPosition = new CameraPosition.Builder() .target(new LatLng(location.getLatitude(), location.getLongitude())).zoom(10).build(); mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); mMessageView.setText("Location = " + new LatLng(location.getLatitude(), location.getLongitude())); (new GetAddressTask(context)).execute(new LatLng(location.getLatitude(), location.getLongitude())); if (location != null) { latitude = "" + location.getLatitude(); longitude = "" + location.getLatitude(); } isMarkerPlaced = true; } } /** * Callback called when connected to GCore. Implementation of {@link ConnectionCallbacks}. */ @Override public void onConnected(Bundle connectionHint) { mLocationClient.requestLocationUpdates(REQUEST, this); // LocationListener } /** * Callback called when disconnected from GCore. Implementation of {@link ConnectionCallbacks}. */ @Override public void onDisconnected() { // Do nothing } /** * Implementation of {@link OnConnectionFailedListener}. */ @Override public void onConnectionFailed(ConnectionResult result) { // Do nothing } @Override public void onMarkerDrag(Marker arg0) { // TODO Auto-generated method stub } @Override public void onMarkerDragEnd(Marker arg0) { mMessageView.setText("Location = " + arg0.getPosition()); latitude = "" + arg0.getPosition().latitude; longitude = "" + arg0.getPosition().longitude; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD && Geocoder.isPresent()) { (new GetAddressTask(context)).execute(arg0.getPosition()); } } @Override public void onMarkerDragStart(Marker arg0) { // TODO Auto-generated method stub } private class GetAddressTask extends AsyncTask<LatLng, Void, String> { Context mContext; public GetAddressTask(Context context) { super(); mContext = context; } @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); NotificationUtils.showProgressDialog(mContext, "Pleasewait", "Getting Location"); } @Override protected void onPostExecute(String address) { // Set activity indicator visibility to "gone" NotificationUtils.dismissProgressDialog(); // Display the results of the lookup. address_text.setText("Address = " + address); strAddress = address; // mAddress.setText(address); } /** * Get a Geocoder instance, get the latitude and longitude look up the * address, and return it * * @params params One or more Location objects * @return A string containing the address of the current location, or * an empty string if no address can be found, or an error * message */ @Override protected String doInBackground(LatLng... params) { Geocoder geocoder = new Geocoder(mContext, Locale.getDefault()); // Get the current location from the input parameter list LatLng loc = params[0]; // Create a list to contain the result address List<Address> addresses = null; try { /* * Return 1 address. */ addresses = geocoder.getFromLocation(loc.latitude, loc.longitude, 1); } catch (IOException e1) { Log.e("LocationSampleActivity", "IO Exception in getFromLocation()"); e1.printStackTrace(); return ("IO Exception trying to get address"); } catch (IllegalArgumentException e2) { // Error message to post in the log String errorString = "Illegal arguments " + Double.toString(loc.latitude) + " , " + Double.toString(loc.longitude) + " passed to address service"; Log.e("LocationSampleActivity", errorString); e2.printStackTrace(); return errorString; } // If the reverse geocode returned an address if (addresses != null && addresses.size() > 0) { // Get the first address Address address = addresses.get(0); /* * Format the first line of address (if available), city, and * country name. */ String addressText = String.format("%s, %s, %s", // If there's a street address, add it address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "", // Locality is usually a city address.getLocality(), // The country of the address address.getCountryName()); // Return the text return addressText; } else { return "No address found"; } } } @Override public void onBackPressed() { Intent returnIntent = new Intent(); returnIntent.putExtra(LATITUDE, latitude); returnIntent.putExtra(LONGITUDE, longitude); returnIntent.putExtra(ADDRESS, strAddress); setResult(RESULT_OK, returnIntent); super.onBackPressed(); overridePendingTransition(R.animator.slide_out_right, R.animator.slide_in_left); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: this.onBackPressed(); return true; } return super.onOptionsItemSelected(item); } }