2017-09-03 11 views
0

私はGoogleスタジオに私の場所を表示するコードを書いていますが、そうしていません。地図は表示されていますが、私が望む座標はマークされていません。Googleマップは現在地を表示していません

public class SEMapTab extends Fragment implements OnMapReadyCallback { 

private View rootView; 
private MapFragment mMapFrag; 
private GoogleMap mMap; 
private LocationManager locationManager; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    if (rootView != null) { 
     ViewGroup parent = (ViewGroup) rootView.getParent(); 
     if (parent != null) 
      parent.removeView(rootView); 
    } 
    try { 
     rootView = inflater.inflate(R.layout.single_entity_map, container, false); 
    } catch (InflateException e) { 

    } 


    return rootView; 
} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    if (mMapFrag == null) { 
     super.onViewCreated(view, savedInstanceState); 
     FragmentManager fragment = getActivity().getFragmentManager(); 

     mMapFrag = (MapFragment) fragment.findFragmentById(R.id.map); 


     mMapFrag.getMapAsync(this); 


    } 

    locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); 
    if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 

    //if network provider is enabled 
    if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) 
    { 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() { 
      @Override 
      public void onLocationChanged(Location location) { 

       double Latitude= location.getLatitude(); //Get latitude 

       double Longitude = location.getLongitude();  //Get longitude 

       LatLng latLng= new LatLng(Latitude, Longitude); 

       //Map latitude and longitude 
       mMap.addCircle(new CircleOptions().center(latLng).fillColor(Color.BLUE).radius(10)); 

       //Zoom in to current location 
       mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,18)); 


      } 

      @Override 
      public void onStatusChanged(String provider, int status, Bundle extras) { 

      } 

      @Override 
      public void onProviderEnabled(String provider) { 

      } 

      @Override 
      public void onProviderDisabled(String provider) { 

      } 
     }); 

    } 
    //else if gps provider is enabled 
    else if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) 
    { 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() { 
      @Override 
      public void onLocationChanged(Location location) { 

       double Latitude= location.getLatitude(); //Get latitude 

       double Longitude = location.getLongitude();  //Get longitude 

       LatLng latLng= new LatLng(Latitude, Longitude); 

       //Map latitude and longitude 
       mMap.addCircle(new CircleOptions().center(latLng).fillColor(Color.BLUE).radius(10)); 

       //Zoom in to current location 
       mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,18)); 



      } 

      @Override 
      public void onStatusChanged(String provider, int status, Bundle extras) { 

      } 

      @Override 
      public void onProviderEnabled(String provider) { 

      } 

      @Override 
      public void onProviderDisabled(String provider) { 

      } 
     }); 
    } 



} 


@Override 
public void onMapReady(GoogleMap googleMap) 
{ 

} 
} 

は、私はまたのAndroidManifest.xmlに必要な権限が

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

地図フラグメントが開いているが、私の現在の位置にマーク取得何のサークルが存在しないファイルに含まれています:コードは次のようです。また、OnMapReady関数でGPS位置をハードコードすると、gettigとマークされて正常に動作しています。私はこの問題を解決するのを助けてくださいますようお願い申し上げます。

+0

LocationManagerによって返された緯度と経度をコンソールに出力します。エミュレータであなたのアプリを動かしていますか? –

+0

私は、エミュレータと電話の両方でそれを実行しています。それは同じ出力です。 – Thanatos

+0

コンソールにはどの座標が表示されていますか? –

答えて

0

onMapReadygoogleMap.setMyLocationEnabled(true)を加えます。ロケーションマネージャとこれらのプロバイダを「手動で」処理する必要はありません。これを呼び出す前にユーザーが自分の場所にアクセスする許可をアプリに与えていることを確認してください。そうしないと、アプリがクラッシュします。

+0

最終的には、この場所(緯度、経度)をデータベースに送信する必要があるため、ロケーションマネージャを使用して行う必要があります。あなたが与えた解決策は実用的な修正ですが、私が書いたコードの問題点は何ですか? – Thanatos

+0

私はこれらのAPIを一度も経験したことがないので、あなたの解決策が間違っているのを知りませんが、私がGoogleApiClientとLocationServicesを使いたいと思っているものを達成するためです。これらのApisは、ユーザーの場所を取得し、場所の更新を聞くためにgoogleによって提案されています。良い例がここにあります:https://github.com/googlesamples/android-play-location/tree/master/LocationUpdates。 Googleは、あなた(開発者)が位置センサーやその他の低レベルのものを処理する必要がないように、これらのすべてを処理しています。 –

+0

エラーはmMap.addCircle()およびmMap.moveCamera()関数にあります。 Log.d( "GPS"、Double.toString(Latitude)+ "" + Double.toString(経度)+ "\ n")を追加すると、GPS座標がデバッグログに出力されます。 。この情報を使用してエラーを特定できるかどうかを確認してください。 – Thanatos

関連する問題