2014-04-16 17 views
11

tôi muốn tạo một chương trình để tính toán khoảng cách giữa một số địa điểm với vị trí hiện tại của tôi, nhưng googleMap.getMyLocation() của tôi; không hoạt động đúng cách.googleMap.getMyLocation(); không thể có được vị trí hiện tại

googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 
googleMap.setMyLocationEnabled(true); 
googleMap.getUiSettings().setCompassEnabled(false); 
mylocation = googleMap.getMyLocation(); 
direction = new GMapV2Direction(); 

LatLng from, to; 
from = new LatLng(-7.26071409, 112.80674726); 
for(int i=0; i<lat.length; i++){  
    to = new LatLng(lat[i], lon[i]); 
    doc = direksi.getDocument(from, to, GMapV2Direction.MODE_DRIVING); 
    distance[i] = (double)direction.getDistanceValue(doc)/1000; 
} 

tôi đã lưu vĩ độ và kinh độ của một số địa điểm trong lat [] và lon []. LatLng 'từ' là vị trí của tôi và 'đến' là nơi đến của tôi. sự cố xuất hiện khi tôi thay đổi

from = new LatLng(-7.26071409, 112.80674726); 
to 
from = new LatLng(mylocation.getLatitude(), mylocation.getLongitude()); 

tôi muốn tính toán mà không cần mở googlemaps. bản đồ google sẽ xuất hiện khi tôi chạm vào nút bản đồ như cửa sổ bật lên. nên việc tính toán sẽ xảy ra mà không cần mở googlemap

hãy giúp tôi

+0

getMyLocation(), [phương pháp này không được chấp nhận] (https://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.html). sử dụng LocationClient để thay thế. – AlexDG

+0

bạn đang sử dụng bản đồ google v2? –

+0

@ Alex tôi không biết cách sử dụng LocationClient. bạn có thể xây dựng? – user3506223

Trả lời

21

Hãy thử điều này ... Tôi hy vọng điều này sẽ giúp cho bạn

LocationManager locationManager = (LocationManager) 
     getSystemService(Context.LOCATION_SERVICE); 
Criteria criteria = new Criteria(); 

Location location = locationManager.getLastKnownLocation(locationManager 
     .getBestProvider(criteria, false)); 
double latitude = location.getLatitude(); 
double longitude = location.getLongitude(); 
+0

có công việc của nó ... tq ... – user3506223

3

getMyLocation(), this method is deprecated. sử dụng LocationClient để thay thế.

Bạn có thể tìm thông tin về LocationClient và làm thế nào để có được vị trí hiện tại here

6

Đây là mã của tôi cho bạn, tôi hy vọng điều này giúp bạn ... chỉnh sửa nó nếu bạn nghĩ rằng, nó không phải là với sintaxis chính xác ...

Tác phẩm này cho android 4.x và 5.x và 6.x, tôi không kiểm tra điều này trong android 7

//I use this like global var to edit or get informacion about google map 
GoogleMap gMap; 

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

if (ActivityCompat.checkSelfPermission(Activity_Map.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(Activity_Map.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
         ActivityCompat.requestPermissions(Activity_Map.this,new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1); 
        }else{ 
         if(!gMap.isMyLocationEnabled()) 
          gMap.setMyLocationEnabled(true); 

         LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
         Location myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

         if (myLocation == null) { 
          Criteria criteria = new Criteria(); 
          criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
          String provider = lm.getBestProvider(criteria, true); 
          myLocation = lm.getLastKnownLocation(provider); 
         } 

         if(myLocation!=null){ 
          LatLng userLocation = new LatLng(myLocation.getLatitude(), myLocation.getLongitude()); 
          gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 14), 1500, null); 
         } 
        } 

} 
+0

cảm ơn bạn.này hoạt động cho tôi. Và đây là một cách tiếp cận tốt. –

Các vấn đề liên quan