你片段类将是:
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" />
他我们去在片段内显示你的地图!请参阅a little post以及the reference来自Google。
对于用户的位置,需要一点阅读才能知道更多关于它的信息:Location Strategies(仔细阅读,一切都在那里)。你需要一个Location Manager
和一个Location Listener
。
首先,你需要这样实现:
// The implement
implements LocationListener { [...] }
有了这个监听器,你就能够拥有并通过GPS或网络更新设备的位置。不要忘了测试设备的服务的状态,这样的事情:
// Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
下面这段代码是这个例子中的一个片段内的当前位置:
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)));
}
}
然后创建经理,然后使用onLocationChanged
方法更新当前位置。有一些很好的教程:a tutorial by Vogella(这可能是帮助)和another tutorial(也可以帮助你)。
您对this blog不少小费。非常简单,非常全面。
注意2:不要忘记在您的清单中添加权限。
我希望,我回答清楚,这将是有益的。
快乐编码!
嘿弗洛,谢谢你的建议。 但我想创建一个片段内的地图视图,说我已创建一个片段类,所以它应该是一个onCreateView方法,而不是的onCreate,因为它是一个片段并不是一项活动... –
酷!我看到了,我根据自己的需要更新了答案。让我知道这是否有帮助。 – Fllo
谢谢你!有用!! –