2011-12-22 156 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