Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. package com.stuff;
  2.  
  3.  
  4. import com.google.android.gms.maps.CameraUpdateFactory;
  5. import com.google.android.gms.maps.GoogleMap;
  6. import com.google.android.gms.maps.SupportMapFragment;
  7. import com.google.android.gms.maps.model.LatLng;
  8. import com.google.android.gms.maps.model.MarkerOptions;
  9.  
  10. import android.location.Criteria;
  11. import android.location.Location;
  12. import android.location.LocationManager;
  13. import android.os.Bundle;
  14. import android.view.Menu;
  15. import android.support.v4.app.FragmentActivity;
  16.  
  17.  
  18. public class MainActivity extends FragmentActivity {
  19.  
  20. private GoogleMap googleMap;
  21.  
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main);
  26. setUpMapIfNeeded();
  27. }
  28.  
  29. private void setUpMapIfNeeded() {
  30. // TODO Auto-generated method stub
  31. if (googleMap == null) {
  32. googleMap = ((SupportMapFragment)
  33. getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
  34. if (googleMap != null) {
  35. setUpMap();
  36. }
  37. }
  38. }
  39.  
  40. private void setUpMap() {
  41. // TODO Auto-generated method stub
  42. googleMap.setMyLocationEnabled(true);
  43.  
  44. LocationManager locationManager = (LocationManager)
  45. getSystemService(LOCATION_SERVICE);
  46. Criteria criteria = new Criteria();
  47. String provider = locationManager.getBestProvider(criteria, true);
  48. Location myLocation = locationManager.getLastKnownLocation(provider);
  49. googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
  50. double latitude = myLocation.getLatitude();
  51. double longitude = myLocation.getLongitude();
  52.  
  53. LatLng latLng = new LatLng(latitude, longitude);
  54. googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
  55. googleMap.animateCamera(CameraUpdateFactory.zoomTo(10));
  56. googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude,
  57. longitude)).title("You are here!"));
  58. }
  59.  
  60. @Override
  61. public boolean onCreateOptionsMenu(Menu menu) {
  62. // Inflate the menu; this adds items to the action bar if it is present.
  63. getMenuInflater().inflate(R.menu.main, menu);
  64. return true;
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement