-5
我在谷歌地圖上找到了以下代碼(第2項)以顯示設備當前位置......效果很好......但是如何將其轉換爲片段以將其添加到現有主要活動。我的大部分代碼使用片段如下:(1項):android將片段活動轉換爲片段
項目1
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new login_login();
break;
case 2:
fragment = new login_create();
break;
case 3:
fragment = new calendar();
break;
case 4:
fragment = new live_webcast();
break;
case 5:
fragment = new live_webcast_archive();
break;
case 6:
fragment = new find_us();
break;
項目2
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class Maps2Activity extends FragmentActivity implements LocationProvider.LocationCallback {
// Use Location Provider
public static final String TAG = Maps2Activity.class.getSimpleName();
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private LocationProvider mLocationProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2_maps);
setUpMapIfNeeded();
mLocationProvider = new LocationProvider(this, this);
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
mLocationProvider.connect();
}
@Override
protected void onPause() {
super.onPause();
mLocationProvider.disconnect();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the find_us.
if (mMap == null) {
// Try to obtain the find_us from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the find_us.
if (mMap != null) {
setUpMap();
}
}
}
/**
* This is where we can add markers or lines, add listeners or move the camera. In this case, we
* just add a marker near Africa.
* <p/>
* This should only be called once and when we are sure that {@link #mMap} is not null.
*/
private void setUpMap() {
//mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
public void handleNewLocation(Location location) {
Log.d(TAG, location.toString());
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
LatLng latLng = new LatLng(currentLatitude, currentLongitude);
commonfunc.myprint("Lat/Lon______________: " + " " + currentLatitude + "-" +currentLongitude);
//mMap.addMarker(new MarkerOptions().position(new LatLng(currentLatitude, currentLongitude)).title("Current Location"));
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("I am here!");
mMap.clear();
mMap.addMarker(options);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
}