Hiển thị các bài đăng có nhãn google map api. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn google map api. Hiển thị tất cả bài đăng

Thứ Ba, 5 tháng 11, 2013

Thứ Tư, 2 tháng 10, 2013

Lấy vị trí thông qua GPS trong android - How to get location in android by gps

Để lấy được vị trí thông qua gps, chúng ta cần import:

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;

và class cần implements LocationListener

Sau đó sử dụng hàm sau để lấy vị trí:

public LatLng getLocation() {
// Get the location manager
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(bestProvider);
Double lat, lon;
try {
lat = location.getLatitude();
lon = location.getLongitude();
return new LatLng(lat, lon);
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
}

--Tuấn Vũ--