2013-05-21 126 views
1

我一直在關注一系列教程,爲使用Google地圖和地圖API v2的android製作基本的餐館定位器應用程序。我遇到的問題是我的gps位置是正確的,但在地圖上繪製的標記顯示用戶位置在用戶實際位置的東南方約60-65英里處。Google Maps API V2標記未顯示在正確的位置

這裏是我的位置類的代碼:

public class GPSTracker extends Service implements LocationListener { 
private final Context mContext; 
boolean isGPSEnabled = false; 
boolean isNetworkEnabled = false; 
boolean canGetLocation = false; 
Location location = null; 
double latitude; 
double longitude; 

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters 
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute 

protected LocationManager locationManager; 

public GPSTracker(Context context) { 
    this.mContext = context; 
    getLocation(); 
} 

public Location getLocation() { 
    try { 
     locationManager = (LocationManager) mContext 
       .getSystemService(LOCATION_SERVICE); 

     isGPSEnabled = locationManager 
       .isProviderEnabled(LocationManager.GPS_PROVIDER); 

     isNetworkEnabled = locationManager 
       .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     if (!isGPSEnabled && !isNetworkEnabled) { 
      // no network provider is enabled 
     } 
     else { 
      this.canGetLocation = true; 
      if (isNetworkEnabled) { 
       locationManager.requestLocationUpdates(
         LocationManager.NETWORK_PROVIDER, 
         MIN_TIME_BW_UPDATES, 
         MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
       Log.d("Network", "Network Enabled"); 
       if (locationManager != null) { 
        location = locationManager 
          .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
        if (location != null) { 
         latitude = location.getLatitude(); 
         longitude = location.getLongitude(); 
        } 
       } 
      } 
      if (isGPSEnabled) { 
       if (location == null) { 
        locationManager.requestLocationUpdates(
          LocationManager.GPS_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.d("GPS", "GPS Enabled"); 
        if (locationManager != null) { 
         location = locationManager 
           .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 
       } 
      } 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return location; 
} 

public void stopUsingGPS() { 
    if (locationManager != null) { 
     locationManager.removeUpdates(GPSTracker.this); 
    } 
} 

public double getLatitude() { 
    if (location != null) { 
     latitude = location.getLatitude(); 
    } 

    return latitude; 
} 

public double getLongitude() { 
    if (location != null) { 
     longitude = location.getLongitude(); 
    } 

    return longitude; 
} 

public boolean canGetLocation() { 
    return this.canGetLocation; 
} 

,並終於在這裏是我的地圖活動代碼:

public class PlacesMapActivity extends FragmentActivity { 
double latitude; 
double longitude; 
LatLng USER=null; 
LatLng LOCATION=null; 
PlacesList nearPlaces; 
private GoogleMap map; 
private int userIcon, locationIcon; 
String address; 
String name; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.map_places); 

    userIcon = R.drawable.mark_red; 
    locationIcon = R.drawable.mark_blue; 

    Intent i = getIntent(); 

    String user_latitude = i.getStringExtra("user_latitude"); 
    String user_longitude = i.getStringExtra("user_longitude"); 
    String user_location = (user_latitude + ", " + user_longitude); 

    SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapView); 
    map = fm.getMap(); 

    USER = new LatLng((int) (Double.parseDouble(user_latitude)), 
      (int) (Double.parseDouble(user_longitude))); 

    Marker user = map.addMarker(new MarkerOptions().position(USER) 
      .title("This is you.") 
      .snippet(user_location) 
      .icon(BitmapDescriptorFactory 
        .fromResource(userIcon))); 

    nearPlaces = (PlacesList) i.getSerializableExtra("near_places"); 
    if(nearPlaces != null) { 
     for(Place place : nearPlaces.results) {    
      latitude = place.geometry.location.lat; 
      longitude = place.geometry.location.lng; 
      name = place.name; 
      address= place.vicinity; 
     } 

     final LatLngBounds.Builder builder = new LatLngBounds.Builder(); 

     for(int c = 0; c < nearPlaces.results.size(); c++){ 
      final LatLng pos = new LatLng(nearPlaces.results.get(c).geometry.location.lat, 
        nearPlaces.results.get(c).geometry.location.lng); 
      builder.include(pos); 
      map.addMarker(new MarkerOptions().position(pos) 
         .title(nearPlaces.results.get(c).name) 
         .snippet(nearPlaces.results.get(c).vicinity) 
         .icon(BitmapDescriptorFactory 
           .fromResource(locationIcon))); 
     } 
    } 

    map.moveCamera(CameraUpdateFactory.newLatLngZoom(USER, 14)); 

    map.animateCamera(CameraUpdateFactory.zoomTo(9), 2000, null); 

}

我試圖附加一個屏幕截圖,顯然我還不能。我設置標記片段以顯示單擊時用戶的緯度和經度。當我點擊用戶標記時,它會顯示正確的經度和緯度(在互聯網上檢查它,它位於我的實際位置的一個塊內),但正如我所說,用戶的標記始終顯示東南方向60-65英里。另外,位置結果的位置都是正確的,並且標記位於正確的位置。

任何幫助,非常感謝。乾杯。

+0

不要將緯度經度轉換爲整數。使用雙重,並檢查 –

+0

哇。作品!我認爲這將是一件非常簡單的事情。非常感謝你! – akenawell85x

+0

將它發佈爲ans:D –

回答

2

在您的代碼如下

USER = new LatLng((int) (Double.parseDouble(user_latitude)), 
     (int) (Double.parseDouble(user_longitude))); 

經緯度沒有轉換爲int,而不是將其作爲保持double

乾杯!