2017-04-25 7 views
1

Googleマップで作業しています。私はユーザーの場所を取得し、マップ上に彼を表示したい。私は、ユーザーの場所を取得するためにLocationListenerを書いた。GPSがオンになっていても場所がnullです

public Location getUserLocation(){ 

    try { 
     // getting GPS status 
     isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
     // getting network status 
     isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
     if (!isGPSEnabled && !isNetworkEnabled) { 
      // no network provider is enabled 

      return null; 
     } else { 
      this.canGetLocation = true; 
      if (isNetworkEnabled) { 
       locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,DISTANCE_CALC,TIME_UPDATE, this); 
       Log.d("Network", "Network Enabled"); 
       if (locationManager != null) { 
        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
        if (location != null) { 
         latitude = location.getLatitude(); 
         longitude = location.getLongitude(); 
        } 
       } 
      } 
      // if GPS Enabled get lat/long using GPS Services 
      if (isGPSEnabled) { 
       if (location == null) { 
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,DISTANCE_CALC,TIME_UPDATE, this); 
        Log.d("GPS", "GPS Enabled"); 
        if (locationManager != null) { 
         location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 
       } 
      } 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return location; 
} 

getUserLocation()はGPSまたはネットワークのいずれかでユーザの位置を戻しますLocationListenerにおける機能の一つです。私はこのような断片からこの関数を呼び出しています。

if (Build.VERSION.SDK_INT >= 23){ 
      if (checkPermission()) { 
       location = locHandler.getUserLocation(); 
       if(location!=null) 
        showTheMaps(location); 
       else 
        AlertBuilder(title,body); 
      }else { 
       requestPermission(); 
      } 
     }else { 
      //The build is less than marshmellow so no need for permission 
      location = locHandler.getUserLocation(); 
      try { 
//    double d = location.getLatitude(); 
       showTheMaps(location); 
      }catch (NullPointerException e){ 
       AlertBuilder(title, body); 
      } 
     } 


public void showTheMaps(Location location){ 

CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),location.getLongitude()), DataModel.zoomlevel); 
googleMap.animateCamera(cameraUpdate); 
} 

AlertBuilder機能は、クリックするとアプリケーションを閉じるボタン付きのアラートボックスを表示します。

スクリーンショットからわかるように、GPSは有効になっていますが、私に返された場所はnullで、警告ダイアログが表示された後にアプリケーションが終了します。 enter image description here 何が問題なのですか?なぜGPSをオンにした後でも、位置がヌルに戻りますか?

+0

https://developer.android.com/training/location/retrieve-current.html

別の(非推奨)の方法は、場所に

mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() { @Override public void onMyLocationChange(Location location) { // do something with location } }); 

詳細情報を得るためにあなたのマップにOnMyLocationChangeListenerを設定することですあなたはlocatを尋ねる頃にGPSに修正がありますか?イオン? – Carcigenicate

+0

私は知らない..アイコンが現れてから遅れている。それはあなたが@ Carcigenicateを意味するものなのだ...アイコンが上に現れる前にダイアログボックスが現れる – Tyson

+1

'requestLocationUpdates()'は非同期)プロセスを実行します。その直後に 'getLastKnownLocation()'を呼び出すと、その場所はまだ利用できません。あなたのクラスは位置リスナを実装し、 'onLocationChanged()'コールバックで位置を受け取るべきです。スタックオーバーフローには多くの例があります。 –

答えて

1

使用

Location currentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 

出典:how to get current location in google map android

+0

非ヌル値を返すための 'getLastLocation()'の必要はありません。このことは、質問には何の改善もありません。 – CommonsWare

+0

OPが場所を取得するのに使用された方法以外の方法を使用することは単なる提案です – Denny

関連する問題