2013-05-15 38 views
0

我把下面的代碼放在我創建的新類中(我希望通過創建一個新類來做正確的事情)。我試圖讓用戶的當前位置顯示在我的地圖上。現在出現的一個問題是,它說「basic_map」不是一個有效的字段。我現在所擁有的只是地圖本身。我希望有一個人可以幫助我。谷歌地圖V2我的位置問題

ps ..對不起,代碼在這裏看起來如何。我似乎無法將它修正爲一直看起來像真正的代碼。

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.LocationSource; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 

import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.widget.Toast; 

public class mylocation extends FragmentActivity implements LocationListener, LocationSource 
{ 
/** 
* Note that this may be null if the Google Play services APK is not available. 
*/ 
private GoogleMap mMap; 

private OnLocationChangedListener mListener; 
private LocationManager locationManager; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.basic_map); 

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 

    if(locationManager != null) 
    { 
     boolean gpsIsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
     boolean networkIsEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     if(gpsIsEnabled) 
     { 
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000L, 10F, this); 
     } 
     else if(networkIsEnabled) 
     { 
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000L, 10F, this); 
     } 
     else 
     { 
      //Show an error dialog that GPS is disabled. 
     } 
    } 
    else 
    { 
     //Show a generic error dialog since LocationManager is null for some reason 
    } 

    setUpMapIfNeeded(); 
} 

@Override 
public void onPause() 
{ 
    if(locationManager != null) 
    { 
     locationManager.removeUpdates(this); 
    } 

    super.onPause(); 
} 

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

    setUpMapIfNeeded(); 

    if(locationManager != null) 
    { 
     mMap.setMyLocationEnabled(true); 
    } 
} 


/** 
* Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly 
* installed) and the map has not already been instantiated.. This will ensure that we only ever 
* call {@link #setUpMap()} once when {@link #mMap} is not null. 
* <p> 
* If it isn't installed {@link SupportMapFragment} (and 
* {@link com.google.android.gms.maps.MapView 
* MapView}) will show a prompt for the user to install/update the Google Play services APK on 
* their device. 
* <p> 
* A user can return to this Activity after following the prompt and correctly 
* installing/updating/enabling the Google Play services. Since the Activity may not have been 
* completely destroyed during this process (it is likely that it would only be stopped or 
* paused), {@link #onCreate(Bundle)} may not be called again so we should call this method in 
* {@link #onResume()} to guarantee that it will be called. 
*/ 
private void setUpMapIfNeeded() { 
    // Do a null check to confirm that we have not already instantiated the map. 
    if (mMap == null) 
    { 
     // Try to obtain the map from the SupportMapFragment. 
     mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 
     // Check if we were successful in obtaining the map. 

     if (mMap != null) 
     { 
      setUpMap(); 
     } 

     //This is how you register the LocationSource 
     mMap.setLocationSource(this); 
    } 
} 

/** 
* 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.setMyLocationEnabled(true); 
} 

@Override 
public void activate(OnLocationChangedListener listener) 
{ 
    mListener = listener; 
} 

@Override 
public void deactivate() 
{ 
    mListener = null; 
} 

@Override 
public void onLocationChanged(Location location) 
{ 
    if(mListener != null) 
    { 
     mListener.onLocationChanged(location); 

     //Move the camera to the user's location once it's available! 
     mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude()))); 
    } 
} 

@Override 
public void onProviderDisabled(String provider) 
{ 
    // TODO Auto-generated method stub 
    Toast.makeText(this, "provider disabled", Toast.LENGTH_SHORT).show(); 
} 

@Override 
public void onProviderEnabled(String provider) 
{ 
    // TODO Auto-generated method stub 
    Toast.makeText(this, "provider enabled", Toast.LENGTH_SHORT).show(); 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) 
{ 
    // TODO Auto-generated method stub 
    Toast.makeText(this, "status changed", Toast.LENGTH_SHORT).show(); 
} 
} 
+0

更新......我改變了的setContentView(R.layout .basic_map); setContentView(R.layout.activity_main);這似乎是唯一可行的。但它仍然不會使我的應用程序處於活動狀態。除此之外,我不知道什麼是錯的。 – Androidinventor

回答

0

試試這個:

setContentView(R.layout.basic_map); 

集地圖:

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 

佈局:basic_map.xml

<fragment 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_below="@id/lltop" 
    android:layout_height="match_parent" 
    class="com.google.android.gms.maps.MapFragment" />