2013-08-20 118 views
1

你好,我需要獲取當前座標,當我開始片段(在onResume())。我嘗試下一步:如何獲取當前位置(經度和緯度)

public class FragmentNear extends Fragment implements LocationListener{ 
View v; 
protected LocationManager locationManager; 
protected LocationListener locationListener; 
protected Context context; 
TextView tvLess1; 
String lat; 
String provider; 
protected String latitude,longitude; 
protected boolean gps_enabled,network_enabled; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    v=inflater.inflate(R.layout.fragment_near, null); 

    tvLess1=(TextView)v.findViewById(R.id.tv_less_1); 
    return v; 
} 

@Override 
public void onResume() { 
    locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 


    super.onResume(); 
} 


@Override 
public void onLocationChanged(Location location) { 
Log.d("myLogs", "onChangeLocation"); 
//tvLess1.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude()); 
//Log.d("myLogs","Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude()); 
} 

@Override 
public void onProviderDisabled(String provider) { 
Log.d("myLogs","disable"); 
} 

@Override 
public void onProviderEnabled(String provider) { 
Log.d("myLogs","enable"); 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
Log.d("myLogs","status"); 
} 
} 

它爲我獲取座標約一分鐘,爲什麼需要這麼多時間?然後onChangeLocation一直在調用,但我只想在onResume調用時更新座標。我該怎麼做?


的解決方案是未來的使用方法:

LocationManager service = (LocationManager)  getActivity().getSystemService(getActivity().LOCATION_SERVICE); 
    Criteria criteria = new Criteria(); 
    String provider = service.getBestProvider(criteria, false); 
    Location location = service.getLastKnownLocation(provider); 
    LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude()); 
+0

你在建築物內嗎? –

+0

不明白 –

回答

0

佔了太多時間,因爲你正在使用GPS

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 

您的設備必須有從GPS satelittes,這是有效信號將根據您的環境情況需要時間。在建築物中需要更多的時間,並且外部的時間會更快,因爲沒有任何設備與衛星之間的直線阻塞。

如果當前GPS位置發生變化,則會一直調用「onLocationChange」方法 - 當設備正在移動或更多的衛星定位被鎖定,以便更精確地跟蹤當前位置時,該方法會經常發生。

爲了使其更快,你可以嘗試不使用GPS:http://developer.android.com/reference/android/location/LocationManager.html

下面是其他可能的方式來讓你的當前位置的列表 - 但是這不會解決「onLocationChange」的連續通話 - 這是正常行爲。如果你不想用這種方法做任何事情,就把它留空。

+0

謝謝,但您可以通過其他方式獲取當前位置嗎? –

+0

我沒有可以顯示的代碼,這些代碼很簡短,可以理解,但您可以查看MyLocationOverlay - 我假設您使用GoogleMaps,因爲您的代碼正在說 - 然後,您可以隨時請求當前位置擁有它。看看https://developers.google.com/maps/documentation/android/v1/reference/com/google/android/maps/MyLocationOverlay這是我在我的應用程序中做到的,它工作得很好,但是作爲我已經告訴過,我無法向您展示簡短易懂的代碼 – glethien

+0

但我使用谷歌地圖v2,並且我沒有使用mapView。 MyLocatonOVerlay(上下文,Mapview) –

0

你可以這樣使用。

@Override 
public void onLocationChanged(Location location) { 
    // TODO Auto-generated method stub 
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
     CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 4); 
     gm.animateCamera(cameraUpdate); 
     Marker myMarker = gm.addMarker(new MarkerOptions() 
     .position(latLng) 
     .title("You are here") 

     .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))); 

     locationManager.removeUpdates(this); 
} 
相關問題