0

현재 위치지도 응용 프로그램을 구현하려고하는데 조각을로드하는 중이지만 주소를 찾기 위해 위도와 경도 값을 사용합니다 .. 현재 위치 기능을 구현하는 방법 ... 미리 감사드립니다.안드로이드에서지도의 현재 위치를 얻는 방법?

방향 및 방법 -

여기 내지도 단편이다 .. 구글의 documentation 가입일

public class ContactFragment extends Fragment { 
MapView mapView; 
GoogleMap googleMap; 
private WebView contactWebView; 
public ContactFragment() { 
} 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_contact, container, false); 
    getActivity().setTitle("Contact Us"); 
    contactWebView = (WebView)view.findViewById(R.id.contact_us); 
    contactWebView.setBackgroundColor(0); 
    if(Build.VERSION.SDK_INT >= 11){ 
     contactWebView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null); 
    } 
    contactWebView.getSettings().setBuiltInZoomControls(true); 
    AssetManager mgr = getContext().getAssets(); 
    String filename = null; 
    try { 
     filename = "contact.html"; 
     System.out.println("filename : " + filename); 
     InputStream in = mgr.open(filename, AssetManager.ACCESS_BUFFER); 
     String sHTML = StreamToString(in); 
     in.close(); 
     contactWebView.loadDataWithBaseURL(null, sHTML, "text/html", "utf-8", null); 
     //singleContent.setText(Html.fromHtml(sHTML)); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    mapView = (MapView) view.findViewById(R.id.map); 
    mapView.onCreate(savedInstanceState); 
    if (mapView != null) { 
     googleMap = mapView.getMap(); 
     googleMap.addMarker(new MarkerOptions() 
       .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)) 
       .anchor(0.5f, 1.0f) 
       .position(new LatLng(12.895960, 77.559921))); 
     googleMap.getUiSettings().setMyLocationButtonEnabled(true); 
     if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

      MapsInitializer.initialize(this.getActivity()); 
      LatLng position = new LatLng(12.895960, 77.559921); 
      CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(position); 

      googleMap.moveCamera(updatePosition); 
      //googleMap.animateCamera(updatePosition); 

      googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(position,16)); 
      return view; 
     } 
     googleMap.setMyLocationEnabled(true); 
     googleMap.getUiSettings().setZoomControlsEnabled(true); 
     MapsInitializer.initialize(this.getActivity()); 
     //   LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
//   builder.include(new LatLng(12.8909537, 77.5594254)); 
//   LatLngBounds bounds = builder.build(); 
     int padding = 0; 
     LatLng position = new LatLng(12.895960, 77.559921); 
     CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(position); 
     CameraUpdate updateZoom = CameraUpdateFactory.zoomBy(8); 
     googleMap.moveCamera(updatePosition); 
     googleMap.animateCamera(updateZoom); 



    } 
    return view; 
} 
@Override 
public void onResume() { 
    mapView.onResume(); 
    super.onResume(); 
} 
@Override 
public void onPause() { 
    super.onPause(); 
    mapView.onPause(); 
} 
@Override 
public void onDestroy() { 
    super.onDestroy(); 
    mapView.onDestroy(); 
} 
@Override 
public void onLowMemory() { 
    super.onLowMemory(); 
    mapView.onLowMemory(); 
} 
public static String StreamToString(InputStream in) throws IOException { 
    if(in == null) { 
     return ""; 
    } 
    Writer writer = new StringWriter(); 
    char[] buffer = new char[1024]; 
    try { 
     Reader reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); 
     int n; 
     while ((n = reader.read(buffer)) != -1) { 
      writer.write(buffer, 0, n); 
     } 
    } finally { 
    } 
    return writer.toString(); 
} 



} 
+1

가능한 중복 http://stackoverflow.com/questions/18425141/android-google-maps-api-v2-zoom-current-location) – naXa

+0

RTFM [Google지도 API | 위치 데이터] (https://developers.google.com/maps/documentation/android-api/location) – naXa

답변

0
if (mMap == null) { 
     // Try to obtain the map from the SupportMapFragment. 
     mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 
     mMap.setMyLocationEnabled(true); 

     if (mMap != null) { 


     mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() { 

     @Override 
     public void onMyLocationChange(Location arg0) { 
     // TODO Auto-generated method stub 

     mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("It's Me!")); 
     } 
     }); 

     } 
    } 
+0

mmap.setMyLocationEnabled (%)에서 권한 확인 오류를 묻습니다. –

+0

AndroidManifest.xml에서 위치 권한을 추가 했습니까? –

+0

예 매니페스트 파일에 대한 모든 권한을 추가했습니다. 조각을 완전히 변경 한 코드를 보내주세요. 감사합니다. –

0

는 안드로이드 장치가 이용할 수있는 위치 데이터 기술의 조합을 사용하여 정확히 장치의 현재 위치를 포함 이동 여부 및 장치가 이동했는지 여부 d는 미리 정의 된 지리적 경계 또는 지오 펜스를 가로 지르게됩니다.

  • 가 장치의 위치를 ​​표시하는 간단한 방법을 제공 내 위치 층 :

    은 응용 프로그램의 필요에 따라 위치 데이터로 작업의 여러 가지 방법 중에서 선택할 수 있습니다 지도. 데이터를 제공하지 않습니다.

  • Google Play 서비스 위치 API은 위치 데이터에 대한 프로그래밍 방식의 모든 요청에 ​​권장됩니다.

  • LocationSource 인터페이스를 사용하면 맞춤 위치 공급자를 제공 할 수 있습니다.

자세한 내용은 그냥 당신이 필요로하는 모든 이들 튜토리얼 및 관련 SO 질문을 설명 할 설명서를 참조하십시오.

[현재 위치에 안드로이드 구글지도 API V2 줌] (의
관련 문제