2011-12-22 61 views
0

我的getLastKnownLocation打開了「null」。我可以獲取我放入的位置以顯示用戶位置一直在嘗試的位置。從文檔中,在這裏以及4 05個教程中,我認爲我做得對,但是......我不是。我似乎無法弄清楚爲什麼。Android的地圖位置返回null

如果有人有一秒鐘來看看這將是偉大的。

package com.kita.Maps; 

import java.util.ArrayList; 
import java.util.List; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.ItemizedOverlay; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapView; 
import com.google.android.maps.MyLocationOverlay; 
import com.google.android.maps.Overlay; 
import com.google.android.maps.OverlayItem; 

import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.net.MailTo; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.widget.Toast; 

public class MapsActivity extends MapActivity implements LocationListener{ 
/** Called when the activity is first created. */ 

MapView map; 
private MyLocationOverlay me = null; 
Drawable d; 
List<Overlay> overlayList; 
LocationManager lm; 
String towers; 
int lat; 
int longi; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    map = (MapView) findViewById(R.id.myMain); 
    map.setBuiltInZoomControls(true); 

    Touchy t = new Touchy(); 
    overlayList = map.getOverlays(); 
    overlayList.add(t); 

    map.getController().setCenter(
      getPoint(40.76793169992044, -73.98180484771729)); 
    map.getController().setZoom(17); 
    map.setBuiltInZoomControls(true); 

    Drawable marker = getResources().getDrawable(R.drawable.pin_yellow); 

    marker.setBounds(0, 0, marker.getIntrinsicWidth(), 
      marker.getIntrinsicHeight()); 

    map.getOverlays().add(new SitesOverlay(marker)); 

    me = new MyLocationOverlay(this, map); 
    map.getOverlays().add(me); 

    d = getResources().getDrawable(R.drawable.pin_blue); 

    // Geo Location of phone 
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    Criteria crit = new Criteria(); 
    towers = lm.getBestProvider(crit, false); 
    Location location = lm.getLastKnownLocation(towers); 

    if(location != null){ 
     lat = (int) (location.getLatitude()*1E6); 
     longi = (int) (location.getLongitude()*1E6); 
     GeoPoint ourLocation = new GeoPoint(lat, longi); 
     OverlayItem overlayItem = new OverlayItem(ourLocation, "Quit hitting your self",""); 
     CustomPinpoint custom = new CustomPinpoint(d, MapsActivity.this); 
     custom.insertPinpoint(overlayItem); 
     overlayList.add(custom); 
    } 
    else{ 
     Toast.makeText(MapsActivity.this, "Couldn't get provider", Toast.LENGTH_SHORT).show(); 
    } 


} 

@Override 
public void onResume() { 
    super.onResume(); 
    me.enableCompass(); 
    lm.requestLocationUpdates(towers, 500, 1, this); 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    me.disableCompass(); 
    lm.removeUpdates(this); 
} 

@Override 
protected boolean isRouteDisplayed() { 
    // TODO Auto-generated method stub 
    return false; 
} 

class Touchy extends Overlay { 
    public boolean onTouchEvent(MotionEvent e, MapView m) { 

     return false; 
    } 
} 

private GeoPoint getPoint(double lat, double lon) { 
    return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0))); 
} 

private class SitesOverlay extends ItemizedOverlay<OverlayItem> { 
    private List<OverlayItem> items = new ArrayList<OverlayItem>(); 

    public SitesOverlay(Drawable marker) { 
     super(marker); 

     boundCenterBottom(marker); 

     items.add(new OverlayItem(getPoint(40.748963847316034, 
       -73.96807193756104), "UN", "United Nations")); 
     items.add(new OverlayItem(getPoint(40.76866299974387, 
       -73.98268461227417), "Lincoln Center", 
       "Home of Jazz at Lincoln Center")); 
     items.add(new OverlayItem(getPoint(40.765136435316755, 
       -73.97989511489868), "Carnegie Hall", 
       "Where you go with practice, practice, practice")); 
     items.add(new OverlayItem(getPoint(40.70686417491799, 
       -74.01572942733765), "The Downtown Club", 
       "Original home of the Heisman Trophy")); 

     populate(); 
    } 

    @Override 
    protected OverlayItem createItem(int i) { 
     return (items.get(i)); 
    } 

    @Override 
    protected boolean onTap(int i) { 
     Toast.makeText(MapsActivity.this, items.get(i).getSnippet(), 
       Toast.LENGTH_SHORT).show(); 

     return (true); 
    } 

    @Override 
    public int size() { 
     return (items.size()); 
    } 
} 



public void onLocationChanged(Location l) { 
    // TODO Auto-generated method stub 

    lat = (int) (l.getLatitude() *1E6); 
    longi = (int) (l.getLongitude()*1E6); 
    GeoPoint ourLocation = new GeoPoint(lat, longi); 
    OverlayItem overlayItem = new OverlayItem(ourLocation, "Quit hitting your self",""); 
    CustomPinpoint custom = new CustomPinpoint(d, MapsActivity.this); 
    custom.insertPinpoint(overlayItem); 
    overlayList.add(custom); 
} 

public void onProviderDisabled(String provider) { 
    // TODO Auto-generated method stub 

} 

public void onProviderEnabled(String provider) { 
    // TODO Auto-generated method stub 

} 

public void onStatusChanged(String provider, int status, Bundle extras) { 
    // TODO Auto-generated method stub 

} 
} 

這裏是我的自定義指針類:

package com.kita.Maps; 

import java.util.ArrayList; 

import android.content.Context; 
import android.graphics.drawable.Drawable; 

import com.google.android.maps.ItemizedOverlay; 
import com.google.android.maps.OverlayItem; 

public class CustomPinpoint extends ItemizedOverlay<OverlayItem>{ 

private ArrayList<OverlayItem> pinpoints = new ArrayList<OverlayItem>(); 
private Context c; 


public CustomPinpoint(Drawable defaultMarker) { 
    super(boundCenter(defaultMarker)); 
    // TODO Auto-generated constructor stub 
} 

public CustomPinpoint(Drawable m, Context context) {   
    // TODO Auto-generated constructor stub 
    this(m); 
    c = context; 
} 

@Override 
protected OverlayItem createItem(int i) { 
    // TODO Auto-generated method stub 
    return pinpoints.get(i); 
} 

@Override 
public int size() { 
    // TODO Auto-generated method stub 
    return pinpoints.size(); 
} 

public void insertPinpoint(OverlayItem item){ 
    pinpoints.add(item); 
    this.populate(); 
} 

} 

回答

1

getLastKnownLocation給出了空值,很多,這只是野獸的性質。

使用LocationListener並等待,如果你想確保穩固的位置。 getLastKnownLocation實際上僅適用於啓動由LocationListener提供的基於位置的邏輯。

+0

這是正確的答案。我把這張支票交給了Ben,因爲提到「requestLocationUpdates」讓我對我的整個應用程序提供了最好的答案。謝謝 – Rick 2011-12-23 19:38:50

1

一般來說,如果你要求探測器的位置(如GPS),並且自從上次啓動以來從未獲取位置,它將返回空值。這就是爲什麼首先請求更新通常會更好,這會導致設備主動找到更新。

一種技術是在您需要它之前找到位置,然後在實際需要時使用getLastKnownLocation。

當使用getLastKnownLocation時,最好:a)盡最大努力確保您使用的位置跟蹤設備已經在啓動過程中找到某個位置,並且b)爲了以防萬一,檢查空位。

作爲一個便箋,我會建議在獲取新位置時檢查準確性。發現的第一個位置並不是那麼準確,同時如果您尋找一段時間,找到的最後一個位置可能不如週期中發現的其他位置那麼精確。關鍵是儘可能少使用GPS,並儘可能控制GPS,並仔細檢查結果。

+0

對不起,我必須把支票交給Ben,requestLocationUpdates將我引導到我的應用的整體長期回答。我投票給你,因爲你的回答告訴我爲什麼它不起作用。希望我可以把它給你。謝謝 – Rick 2011-12-23 19:42:13

+0

沒問題,我很肯定他先回答,我只是想澄清一下大家的理解。教一個男人去釣魚。 – Pyrodante 2011-12-23 19:43:29