آموزش های این وب سایت به صورت رایگان در دسترس است. اطلاعات بیشتر
بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]

چرا متد onLocationChanged فراخوانی نمی شود ؟

kazemian  8 سال پیش  8 سال پیش
0 0

سلام بچه ها
من میخوام موقعیت کاربر رو بگیرم و با توجه به موقعیتش و نقطه ای  که روی نقشه انتخاب می کنیم عمل مسیریابی رو انجام بدم
اما اصلا متد onLocationChanged  فراخوانی نمیشه!
دلیلش چی میتونه باشه و چه راه حلی پیشنهاد میدین ؟

 
public class MapOriginal extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
private LatLng origen, destination;
double latX, lonY;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_original);

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map2);
mapFragment.getMapAsync(this);


LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {

@Override
public void onLocationChanged(Location location) {
latX = location.getLatitude();
lonY = location.getLongitude();
origen = new LatLng(latX, lonY);
Log.i("origen", origen + "test");
mMap.addMarker(new MarkerOptions().position(origen).title("Marker in Lamered"));
}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}
};



if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
// locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);


}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

// mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(lamerd, 16));

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(origen, 15.5f), 4000, null);
mMap.getUiSettings().setZoomControlsEnabled(true);
// type of map
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

// create circle shape on the map
CircleOptions circleOptions = new CircleOptions().center(origen).radius(100).strokeColor(Color.CYAN);
Circle circle = mMap.addCircle(circleOptions);

// create Polygon shape on the map
PolygonOptions polygonOptions = new PolygonOptions().add(new LatLng(27.334231, 53.185669), new LatLng(27.336307, 53.187374), new LatLng(27.336563, 53.188213), new LatLng(27.335746, 53.189575), new LatLng(27.333886, 53.188321)).strokeColor(Color.GREEN);
Polygon polygon = mMap.addPolygon(polygonOptions);


mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
// mMap.clear();
destination = new LatLng(latLng.latitude, latLng.longitude);
mMap.addMarker(new MarkerOptions().position(destination).title("Marker in Lamered"));

double distance = 0;
Location location1 = new Location("A");
location1.setLatitude(origen.latitude);
location1.setLongitude(origen.longitude);

Location location2 = new Location("B");
location2.setLatitude(destination.latitude);
location2.setLongitude(destination.longitude);

distance = location1.distanceTo(location2);
double KM = (int) distance / 1000;
double Meter = (int) distance % 1000;

Toast.makeText(getApplicationContext(), "KM: " + KM + " Meter: " + Meter, Toast.LENGTH_LONG).show();

String serverKey = "my server key";
GoogleDirection.withServerKey(serverKey)
.from(origen)
.to(destination)
.transitMode(TransportMode.DRIVING)
.execute(new DirectionCallback() {
@Override
public void onDirectionSuccess(Direction direction, String rawBody) {
String status = direction.getStatus();
if (direction.isOK()) {
Route route = direction.getRouteList().get(0);
Leg leg = route.getLegList().get(0);
ArrayList<LatLng> directionPositionList = leg.getDirectionPoint();
PolylineOptions polygonOptions = DirectionConverter.createPolyline(getApplicationContext(), directionPositionList, 5, Color.RED);
mMap.addPolyline(polygonOptions);
}
}

@Override
public void onDirectionFailure(Throwable t) {
// Do something here
}
});
}
});
}
}
 برای این سوال پاسخی وجود ندارد.

پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .