귀하의 조각 클래스이 될 것입니다 :
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 post 및 the reference을 참조하십시오.
사용자의 위치에 대한
, 독서의 약간은 그것에 대해 더 알고, 필요 : Location Strategies는 (주의 깊게 읽고, 모두가 얘기입니다). Location Manager
및 Location 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 :는 매니페스트에 권한을 추가하는 것을 잊지 마십시오.
나는 분명히 대답했고 이것이 유용 할 것이라고 희망한다.
해피 코딩!
안녕하세요. Fllo, 감사합니다. 하지만 프래그먼트 안에 맵보기를 만들려면 프래그먼트 클래스를 만들어야하므로 onCreate 메서드 대신 onCreate 메서드를 사용해야합니다. 이는 조각이 아니기 때문에 ... –
Hep! 나는 당신의 필요에 따라 대답을 업데이트했다. 이것이 도움이되는지 알려주세요. – Fllo
고마워요! 그것은 작동! –