Guest User

Untitled

a guest
Jun 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. public static double lon;
  2. public static double lat;
  3.  
  4.         @Override
  5.         public void onCreate(Bundle savedInstanceState) {
  6.                 super.onCreate(savedInstanceState);
  7.                 setContentView(R.layout.main);
  8.  
  9.         final LocationManager locmanager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
  10.         final LocationListener locationListener = new MyLocationListener();
  11.         locmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
  12.  
  13.                 mapView = (MapView) findViewById(R.id.mapview);
  14.                 mapView.setBuiltInZoomControls(true);
  15.  
  16.                 new Thread() {
  17.                         @Override
  18.                         public void run()
  19.             {
  20.                
  21.                 double fromLat = lat, fromLon = lon, toLat = 50.84919869326253, toLon = 5.7025909423828125;
  22.                             //double fromLat = 51.43731661373785, fromLon = 5.472564697265625, toLat = 50.84919869326253, toLon = 5.7025909423828125;
  23.  
  24.                                 String url = RoadProvider
  25.                                                 .getUrl(fromLat, fromLon, toLat, toLon);
  26.                                 InputStream is = getConnection(url);
  27.                                 mRoad = RoadProvider.getRoute(is);
  28.                                 mHandler.sendEmptyMessage(0);
  29.                         }
  30.                 }.start();
  31.         }
  32.  
  33. class MyLocationListener implements LocationListener {
  34.  
  35.     public void onLocationChanged(Location location) {
  36.         MapRouteActivity.lon = location.getLongitude();
  37.         MapRouteActivity.lat = location.getLatitude();
  38.         //Toast.makeText(getApplicationContext(), "Lat: " + location.getLatitude() + " Long: " + location.getLatitude())
  39.  
  40.     }
  41.  
  42.     public void onStatusChanged(String s, int i, Bundle bundle) {
  43.  
  44.     }
  45.  
  46.     public void onProviderEnabled(String s) {
  47.  
  48.     }
  49.  
  50.     public void onProviderDisabled(String s) {
  51.  
  52.     }
  53. }
  54.  
  55.  
  56.  
  57. Het gaat fout bij het gedeelte waar de thread start en deze verwacht dan dat de lon en lat coordinaten al beschikbaar zijn.
  58.  
  59. Een eventuele oplossing is dat de thread wordt gesynchroniseerd of wacht totdat de gps klaar is met een positie ophalen.
  60.  
  61. De thread mag pas starten als de gps verbinding aanwezig is.
  62. Het belangrijkste gedeelte is namelijk waar de nieuwe thread wordt gerunt. De code daarin mag niet veranderen, hier mag wel het GPS ophalen worden afgehandeld.
Add Comment
Please, Sign In to add comment