如何获得在谷歌地图API V2设备的当前位置? [英] How to get current location of device on google maps api v2?
问题描述
从来就找到了一些方法来做到这一点但德precated或doesn't工作。我想获得设备当前的经纬度。
下面是我应得的当前位置,但GoogleMap的 getMyLocation()
方法<一href=\"https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.html#getMyLocation()\"相对=nofollow>是德precated :
无效getCurrentLocation()
{
位置myLocation = map.getMyLocation();
如果(myLocation!= NULL)
{
双dLatitude = myLocation.getLatitude();
双dLongitude = myLocation.getLongitude();
map.addMarker(新的MarkerOptions()位置(经纬度新(dLatitude,dLongitude))
.title伪(我的位置)。图标(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(新经纬度(dLatitude,dLongitude),8)); }
其他
{
Toast.makeText(这一点,无法获取当前位置,Toast.LENGTH_SHORT).show();
}
}
这其实很简单,使用FusedLocationProviderAPI建议在使用较早的开放源码的位置API,特别是因为你已经在使用谷歌地图,这样你已经使用谷歌播放服务。
只需设置一个位置监听器,并在每个 onLocationChanged()
回调更新您的当前位置标记。如果你只想要一个位置更新,只需注销第一个回调返回后回调。
公共类MainActivity扩展FragmentActivity
实现OnMa preadyCallback,
GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,
LocationListener的{ 私人GoogleMap的地图;
私人LocationRequest mLocationRequest;
私人GoogleApiClient mGoogleApiClient;
私人位置mLastLocation;
私人标记标记; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); } @覆盖
保护无效onResume(){
super.onResume(); 如果(mGoogleApiClient == NULL ||!mGoogleApiClient.isConnected()){ buildGoogleApiClient();
mGoogleApiClient.connect(); } 如果(图== NULL){
MapFragment mapFragment =(MapFragment)getFragmentManager()
.findFragmentById(R.id.map); mapFragment.getMapAsync(本);
}
} @覆盖
公共无效onMa pready(GoogleMap的retMap){ 地图= retMap; setUpMap(); } 公共无效setUpMap(){ map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.setMyLocationEnabled(真);
} @覆盖
保护无效的onPause(){
super.onPause();
如果(mGoogleApiClient!= NULL){
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,这一点);
}
} 保护同步无效buildGoogleApiClient(){
Toast.makeText(这一点,buildGoogleApiClient,Toast.LENGTH_SHORT).show();
mGoogleApiClient =新GoogleApiClient.Builder(本)
.addConnectionCallbacks(本)
.addOnConnectionFailedListener(本)
.addApi(LocationServices.API)
。建立();
} @覆盖
公共无效onConnected(束束){
Toast.makeText(这一点,onConnected,Toast.LENGTH_SHORT).show(); mLocationRequest =新LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); //mLocationRequest.setSmallestDisplacement(0.1F); LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,这一点);
} @覆盖
公共无效onConnectionSuspended(int i)以{ } @覆盖
公共无效onConnectionFailed(ConnectionResult connectionResult){ } @覆盖
公共无效onLocationChanged(地点){
mLastLocation =位置; //删除previous当前位置标记
如果(标记!= NULL){
marker.remove();
} 双dLatitude = mLastLocation.getLatitude();
双dLongitude = mLastLocation.getLongitude();
标记= map.addMarker(新的MarkerOptions()位置(经纬度新(dLatitude,dLongitude))
.title伪(我的位置)。图标(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(新经纬度(dLatitude,dLongitude),8)); }}
I´ve found some methods to do that but are deprecated or doesn´t work. I would like to get current latitude and longitude from device.
Here is how I'm getting the current location, but the GoogleMap getMyLocation()
method is deprecated:
void getCurrentLocation()
{
Location myLocation = map.getMyLocation();
if(myLocation!=null)
{
double dLatitude = myLocation.getLatitude();
double dLongitude = myLocation.getLongitude();
map.addMarker(new MarkerOptions().position(new LatLng(dLatitude, dLongitude))
.title("My Location").icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 8));
}
else
{
Toast.makeText(this, "Unable to fetch the current location", Toast.LENGTH_SHORT).show();
}
}
It's actually quite simple, using the FusedLocationProviderAPI is recommended over using the older open source Location APIs, especially since you're already using a Google Map so you are already using Google Play Services.
Simply set up a Location Listener, and update your current location Marker in each onLocationChanged()
callback. If you only want one location update, just un-register for callbacks after the first callback returns.
public class MainActivity extends FragmentActivity
implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
LocationListener {
private GoogleMap map;
private LocationRequest mLocationRequest;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
private Marker marker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
if (mGoogleApiClient == null || !mGoogleApiClient.isConnected()){
buildGoogleApiClient();
mGoogleApiClient.connect();
}
if (map == null) {
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap retMap) {
map = retMap;
setUpMap();
}
public void setUpMap(){
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.setMyLocationEnabled(true);
}
@Override
protected void onPause(){
super.onPause();
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
protected synchronized void buildGoogleApiClient() {
Toast.makeText(this, "buildGoogleApiClient", Toast.LENGTH_SHORT).show();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnected(Bundle bundle) {
Toast.makeText(this,"onConnected", Toast.LENGTH_SHORT).show();
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
//mLocationRequest.setSmallestDisplacement(0.1F);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
//remove previous current location Marker
if (marker != null){
marker.remove();
}
double dLatitude = mLastLocation.getLatitude();
double dLongitude = mLastLocation.getLongitude();
marker = map.addMarker(new MarkerOptions().position(new LatLng(dLatitude, dLongitude))
.title("My Location").icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 8));
}
}
这篇关于如何获得在谷歌地图API V2设备的当前位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!