映射位置为android返回null

时间:2011-12-22 18:51:42

标签: android google-maps

我的getLastKnownLocation正在变为“null”。我可以获取我放置的位置但是获取用户位置一直在尝试。从文档,这里和4 05教程我认为我做得对,但......我不是。我似乎无法弄明白为什么。

如果某人有一秒钟来看这个会很棒。

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();
}

}

3 个答案:

答案 0 :(得分:1)

尝试requestLocationUpdates而不是getLastKnownLocation。您可以使用LocationListener来侦听位置更新。更多信息:https://developer.android.com/reference/android/location/LocationListener.html

答案 1 :(得分:1)

getLastKnownLocation给出了空值,很多,这只是野兽的本质。

使用LocationListener并等待,如果您想确保稳固的位置。 getLastKnownLocation实际上只适用于通过LocationListener提供基于位置的逻辑。

答案 2 :(得分:1)

一般来说,最新情况是,如果您请求探测器(如GPS)的位置,并且自上次启动以来它从未获得过位置,它将返回null。这就是为什么通常最好先请求更新,这将导致设备主动查找更新。

一种技术是在您需要之前找到位置,然后在实际需要时使用getLastKnownLocation。

使用getLastKnownLocation时,最好a)尽最大努力确保您使用的位置跟踪设备已经在此引导过程中找到了某个位置,并且b)您检查是否为空以防万一。

作为附注,我建议在获取新位置时检查准确性。很常见的是,找到的第一个位置并不尽可能准确,同时如果您查找一段时间,找到的最后位置可能不如周期中找到的其他位置那么准确。关键是尽可能少地使用GPS,并仔细控制,并仔细检查您的结果。