Guest User

Untitled

a guest
Jul 19th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. import com.google.android.maps.MapActivity;
  2. import com.google.android.maps.MapView;
  3. import com.google.android.maps.MapController;
  4. import com.google.android.maps.GeoPoint;
  5. import android.content.Context;
  6. import android.location.Location;
  7. import android.location.LocationListener;
  8. import android.location.LocationManager;
  9.  
  10. import android.location.Geocoder;
  11. import android.location.Address;
  12.  
  13. import com.google.android.maps.Overlay;
  14. import android.graphics.Canvas;
  15. import android.graphics.Point;
  16. import android.graphics.Bitmap;
  17. import android.graphics.BitmapFactory;
  18.  
  19. import java.util.List;
  20. import java.util.Locale;
  21. import java.io.IOException;
  22.  
  23. import android.os.Bundle;
  24. import android.view.MotionEvent;
  25. import android.widget.Toast;
  26.  
  27. public class GPSLocatorActivity extends MapActivity {
  28. private MapView mapView;
  29. private MapController mapController;
  30.  
  31. private LocationManager locationManager;
  32. private LocationListener locationListener;
  33.  
  34. @Override
  35. public void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.main);
  38.  
  39. locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  40.  
  41. locationListener = new GPSLocationListener();
  42.  
  43. locationManager.requestLocationUpdates(
  44. LocationManager.GPS_PROVIDER,
  45. 0,
  46. 0,
  47. locationListener);
  48.  
  49. mapView = (MapView) findViewById(R.id.mapView);
  50.  
  51.  
  52. // enable Street view by default
  53. // mapView.setStreetView(true);
  54.  
  55. // enable to show Satellite view
  56. mapView.setSatellite(true);
  57.  
  58. // enable to show Traffic on map
  59. // mapView.setTraffic(true);
  60. mapView.setBuiltInZoomControls(true);
  61.  
  62. mapController = mapView.getController();
  63. mapController.setZoom(17);
  64. mapView.invalidate();
  65.  
  66. }
  67.  
  68. @Override
  69. protected boolean isRouteDisplayed() {
  70. return false;
  71. }
  72.  
  73. private class GPSLocationListener implements LocationListener
  74. {
  75. @Override
  76. public void onLocationChanged(Location location) {
  77. if (location != null) {
  78. GeoPoint point = new GeoPoint(
  79. (int) (location.getLatitude() * 1E6),
  80. (int) (location.getLongitude() * 1E6));
  81. /*
  82. //Toast.makeText(getBaseContext(),
  83. Toast.makeText(GPSLocatorActivity.this,
  84. "Latitude: " + location.getLatitude() +
  85. " Longitude: " + location.getLongitude(),
  86. Toast.LENGTH_LONG).show();
  87. */
  88. //mapController.setZoom(17);
  89. mapController.animateTo(point);
  90. mapController.setZoom(17);
  91.  
  92. // add marker
  93. MapOverlay mapOverlay = new MapOverlay();
  94. mapOverlay.setPointToDraw(point);
  95. List<Overlay> listOfOverlays = mapView.getOverlays();
  96. listOfOverlays.clear();
  97. listOfOverlays.add(mapOverlay);
  98.  
  99. String address = ConvertPointToLocation(point);
  100. Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();
  101.  
  102. mapView.invalidate();
  103. }
  104. }
  105.  
  106. public String ConvertPointToLocation(GeoPoint point) {
  107. String address = "";
  108. Geocoder geoCoder = new Geocoder(
  109. getBaseContext(), Locale.getDefault());
  110. try {
  111. List<Address> addresses = geoCoder.getFromLocation(
  112. point.getLatitudeE6() / 1E6,
  113. point.getLongitudeE6() / 1E6, 1);
  114.  
  115. if (addresses.size() > 0) {
  116. for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
  117. address += addresses.get(0).getAddressLine(index) + " ";
  118. }
  119. }
  120. catch (IOException e) {
  121. e.printStackTrace();
  122. }
  123.  
  124. return address;
  125. }
  126.  
  127. @Override
  128. public void onProviderDisabled(String provider) {
  129. }
  130.  
  131. @Override
  132. public void onProviderEnabled(String provider) {
  133. }
  134.  
  135. @Override
  136. public void onStatusChanged(String provider, int status, Bundle extras) {
  137. }
  138. }
  139.  
  140. class MapOverlay extends Overlay
  141. {
  142. private GeoPoint pointToDraw;
  143.  
  144. public void setPointToDraw(GeoPoint point) {
  145. pointToDraw = point;
  146. }
  147.  
  148. public GeoPoint getPointToDraw() {
  149. return pointToDraw;
  150. }
  151.  
  152.  
  153. @Override
  154. public boolean onTouchEvent(MotionEvent e, MapView mapView) {
  155. // TODO Auto-generated method stub
  156. return super.onTouchEvent(e, mapView);
  157. //mapView.invalidate();
  158. //return false;
  159.  
  160. }
  161.  
  162. @Override
  163. public boolean onTap(GeoPoint p, MapView mapView) {
  164. // TODO Auto-generated method stub
  165. //mapView.invalidate();
  166. return super.onTap(p, mapView);
  167. }
  168.  
  169. @Override
  170. public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
  171. super.draw(canvas, mapView, shadow);
  172.  
  173. // convert point to pixels
  174. Point screenPts = new Point();
  175. mapView.getProjection().toPixels(pointToDraw, screenPts);
  176.  
  177. // add marker
  178. Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.red);
  179. canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null); // 24 is the height of image
  180. return true;
  181. }
  182.  
  183. }
  184. }
  185.  
  186. public class GPSLocatorActivity extends MapActivity {
  187. Context mContext = this;
  188.  
  189. Toast.makeText(mContext, "some_string", Toast.LENGTH_LONG).show();
  190.  
  191. Toast.makeText(GPSLocatorActivity.this,
  192. Toast.makeText(GPSLocatorActivity.this,
  193. "Latitude: " + location.getLatitude() +
  194. " Longitude: " + location.getLongitude(),
  195. Toast.LENGTH_LONG).show();
Add Comment
Please, Sign In to add comment