2017-09-03 1 views
0

Google지도 조각에 내 위치를 표시하기 위해 Android 스튜디오에 코드를 작성했지만 표시하지 않습니다. 지도가 표시되고 있지만 원하는 좌표가 표시되지 않습니다.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를 사용하여 원하는 것을 성취하고자합니다. 이 API는 사용자의 위치를 ​​얻고 위치 업데이트를 수신하기 위해 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

관련 문제