Advertisement
Guest User

Untitled

a guest
Aug 30th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.34 KB | None | 0 0
  1. package com.example.bro.gpsdemo;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.graphics.Point;
  6. import android.location.Location;
  7. import android.location.LocationListener;
  8. import android.location.LocationManager;
  9. import android.net.Uri;
  10. import android.provider.Settings;
  11. import android.support.v4.app.FragmentActivity;
  12. import android.os.Bundle;
  13.  
  14. import com.google.android.gms.maps.CameraUpdate;
  15. import com.google.android.gms.maps.CameraUpdateFactory;
  16. import com.google.android.gms.maps.GoogleMap;
  17. import com.google.android.gms.maps.GoogleMapOptions;
  18. import com.google.android.gms.maps.MapFragment;
  19. import com.google.android.gms.maps.MapView;
  20. import com.google.android.gms.maps.SupportMapFragment;
  21. import com.google.android.gms.maps.UiSettings;
  22. import com.google.android.gms.maps.model.CameraPosition;
  23. import com.google.android.gms.maps.model.LatLng;
  24. import com.google.android.gms.maps.model.LatLngBounds;
  25. import com.google.android.gms.maps.model.MarkerOptions;
  26. import com.google.android.gms.maps.model.VisibleRegion;
  27.  
  28. public class MapsActivity extends FragmentActivity {
  29.  
  30.     private GoogleMap mMap; // Might be null if Google Play services APK is not available.
  31.  
  32.  
  33.  
  34.     @Override
  35.     protected void onCreate(Bundle savedInstanceState) {
  36.         super.onCreate(savedInstanceState);
  37.         setContentView(R.layout.activity_maps);
  38.         setUpMapIfNeeded();
  39.         mMap.setMyLocationEnabled(true);
  40.         mMap.animateCamera(CameraUpdateFactory.zoomTo(12));
  41.  
  42.  
  43.  
  44.        final LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
  45.        LocationListener listener = new LocationListener() {
  46.  
  47.            @Override
  48.            public void onLocationChanged(Location location) {
  49.                LatLng current = new LatLng(location.getLatitude(),location.getLongitude());
  50.                mMap.moveCamera(CameraUpdateFactory.newLatLng(current));
  51.  
  52.  
  53.  
  54.            }
  55.  
  56.            @Override
  57.            public void onStatusChanged(String provider, int status, Bundle extras) {
  58.  
  59.            }
  60.  
  61.            @Override
  62.            public void onProviderEnabled(String provider) {
  63.  
  64.  
  65.            }
  66.  
  67.            @Override
  68.            public void onProviderDisabled(String provider) {
  69.  
  70.            }
  71.        };
  72.  
  73.  
  74.         manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,listener);
  75.  
  76.  
  77.     }
  78.  
  79.  
  80.     @Override
  81.     protected void onResume() {
  82.         super.onResume();
  83.         setUpMapIfNeeded();
  84.     }
  85.  
  86.     /**
  87.      * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
  88.      * installed) and the map has not already been instantiated.. This will ensure that we only ever
  89.      * call {@link #setUpMap()} once when {@link #mMap} is not null.
  90.      * <p>
  91.      * If it isn't installed {@link SupportMapFragment} (and
  92.      * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
  93.      * install/update the Google Play services APK on their device.
  94.      * <p>
  95.      * A user can return to this FragmentActivity after following the prompt and correctly
  96.      * installing/updating/enabling the Google Play services. Since the FragmentActivity may not
  97.      * have been completely destroyed during this process (it is likely that it would only be
  98.      * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
  99.      * method in {@link #onResume()} to guarantee that it will be called.
  100.      */
  101.     private void setUpMapIfNeeded() {
  102.         // Do a null check to confirm that we have not already instantiated the map.
  103.         if (mMap == null) {
  104.             // Try to obtain the map from the SupportMapFragment.
  105.             mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
  106.                     .getMap();
  107.             // Check if we were successful in obtaining the map.
  108.             if (mMap != null) {
  109.                 setUpMap();
  110.             }
  111.         }
  112.     }
  113.  
  114.     /**
  115.      * This is where we can add markers or lines, add listeners or move the camera. In this case, we
  116.      * just add a marker near Africa.
  117.      * <p>
  118.      * This should only be called once and when we are sure that {@link #mMap} is not null.
  119.      */
  120.     private void setUpMap() {
  121.         mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement