2014-03-04 2 views
0

다음은 내 예입니다. 안드로이드에서 주된 활동을하고 툴바를 만들었습니다. 위치 함수라고 부르는 location이라는 하위 함수가 있습니다. 이리. 툴바에서 아이콘을 클릭하면 하위 기능을 사용할 때 조각을 만들어야하기 때문에 조각으로 코딩되어 있습니다.조각 내 Google지도 기능을 넣는 방법

나는 활동 내에서 Google지도를 표시하는 방법을 알아 냈습니다. 그러나 나는 그것을 단편에서 보여주지 못했다. 내가 할 수있는 것은 단편 안에서 onActivityCreated를 호출하는 것입니다. 그러나, 프로그램은 잘 작동하지만 레이아웃은 도구 모음을 생략 할 것입니다. (영어에 능숙하지 않습니다. 내가 말하는 것에 대해 이해하지 못하는 경우 자세한 내용을 묻습니다 :)

누구든지 png와 lat를 찾거나 조각 내 Google지도를 보여주는 것과 같은 일부 Google 위치 서비스를 호출하는 방법을 알아낼 수 있습니까? 내 조각 코드는 아래에 있습니다

LocationFragment.java :

public class LocationFragment extends MapFragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View location_layout = inflater.inflate(R.layout.location_layout, container, false); 
     return location_layout; 
    } 
} 

답변

1

귀하의 조각 클래스이 될 것입니다 :

public class MyFragment extends Fragment { 

    private MapView mapView; 
    private GoogleMap map; 
    private Bundle bundle; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.fragment_map, container, false); 

     // initialize it to avoid a NPE 
     try { 
      MapsInitializer.initialize(getActivity()); 
     } catch (GooglePlayServicesNotAvailableException e) { } 

     mapView = (MapView) v.findViewById(R.id.map); 
     mapView.onCreate(bundle); 

     // check if the map is null 
     if (map == null) { 
      map = ((MapView) v.findViewById(R.id.map)).getMap(); 
     } 

     // add a marker 
     map.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Here!")); 

     return v; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     bundle = savedInstanceState; 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     mapView.onResume(); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     mapView.onPause(); 
    } 

    @Override 
    public void onDestroy() { 
     mapView.onDestroy(); 
     super.onDestroy(); 
    } 

    @Override 
    public void onLowMemory() { 
     super.onLowMemory(); 
     mapView.onLowMemory(); 
    } 
} 

그리고 마지막으로, 레이아웃 fragment_map :

<com.google.android.gms.maps.MapView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/map" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 

그는 '에 가서 조각 내지도를 보여줍니다! Google의 a little postthe reference을 참조하십시오.

사용자의 위치에 대한

, 독서의 약간은 그것에 대해 더 알고, 필요 : Location Strategies는 (주의 깊게 읽고, 모두가 얘기입니다). Location ManagerLocation Listener이 필요합니다. 모든
첫째, 당신은이 구현해야

// The implement 
implements LocationListener { [...] } 

청취자에게, 당신은 것 가지고 GPS 또는 네트워크를 통해 장치의 위치를 ​​업데이트 할 수.

public class LocationFragment extends Fragment implements LocationListener { 

    private Marker marker; 
    private LocationManager locationManager; 

    static final LatLng NEWYORK = new LatLng(40.75, -74.03); 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     super.onCreateView(inflater, container, savedInstanceState); 
     View v = inflater.inflate(R.layout.location_layout, container, false); 

     // ... 
     if (map == null) { 
      map = ((MapView) v.findViewById(R.id.map)).getMap(); 
     } 
     // ... 
     if (map != null) initMap(); 

     locationManager = (LocationManager) getActivity().getApplicationContext().getSystemService(Context.LOCATION_SERVICE); 
     if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
      getGPS(); 
     } else { 
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 10, this);  
     } 
     // ... 

     return v; 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     getGPS(); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     outGPS(); 
    } 

    public void getGPS() { 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this); 
    } 

    public void outGPS() { 
     locationManager.removeUpdates(this); 
    } 

    @Override 
    public void onLocationChanged(final Location location) { 
     // get the latitude and the longitude 
     final StringBuilder msg = new StringBuilder("lat : "); 
     msg.append(location.getLatitude()); 
     msg.append("; lng : "); 
     msg.append(location.getLongitude()); 
     // display it on a toast 
     Toast.makeText(getActivity().getApplicationContext(), msg.toString(), Toast.LENGTH_SHORT).show(); 

     // final latlng with these above to update the position on a marker 
     final LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
     gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 1)); 
     marker.setPosition(latLng); 
     // ... 
    } 

    @Override 
    public void onProviderDisabled(final String provider) { 
     if("gps".equals(provider)) { 
      desabonnementGPS(); 
     } 
    } 

    @Override 
    public void onProviderEnabled(final String provider) { 
     if("gps".equals(provider)) { 
      abonnementGPS(); 
     } 
    } 

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

    private void initMap() { 
     gMap.addMarker(new MarkerOptions().title("New-York").position(NEWYORK)); 
     marker = gMap.addMarker(new MarkerOptions().title("I am here!").position(new LatLng(0, 0))); 
    } 
} 

그런 다음 당신이를 만듭니다

// Getting Google Play availability status 
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); 

아래에이 코드 조각 내부의 현재 위치를 얻을 수있는 예입니다 :이 같은 장치의 서비스 상태, 뭔가를 테스트하는 것을 잊지 마세요 현재 위치를 onLocationChanged 메소드로 업데이트 한 후 거기에 대한 좋은 자습서 : a tutorial by Vogella (도움이 될 수도) 및 another tutorial (당신을 도울 수).

당신은 this blog에 대한 도움말을 많이 가지고 있어요. 매우 간단하고 매우 포괄적입니다.

NOTE2 :는 매니페스트에 권한을 추가하는 것을 잊지 마십시오.

나는 분명히 대답했고 이것이 유용 할 것이라고 희망한다.
해피 코딩!

+0

안녕하세요. Fllo, 감사합니다. 하지만 프래그먼트 안에 맵보기를 만들려면 프래그먼트 클래스를 만들어야하므로 onCreate 메서드 대신 onCreate 메서드를 사용해야합니다. 이는 조각이 아니기 때문에 ... –

+0

Hep! 나는 당신의 필요에 따라 대답을 업데이트했다. 이것이 도움이되는지 알려주세요. – Fllo

+0

고마워요! 그것은 작동! –

관련 문제