地图位置返回android的null [英] Map location returning null for android

查看:62
本文介绍了地图位置返回android的null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的getLastKnownLocation变成空".我可以输入要显示的位置,但是要尝试获取用户的位置.从文档,这里和4个05教程中,我以为我做得对,但是……我不对.我只是似乎不知道为什么.

My getLastKnownLocation is turning up "null". I can get the locations I put in to show however get the users location has been trying. From the docs, here, and 4 05 tutorials I thought I was doing it right, but...... I'm not. I just cannot seem to figure out why.

如果某人有时间仔细检查,那将是很好的.

If someone has a sec to look this over that would be great.

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

}
}

这是我的自定义指针类:

Here is my custom pointer class:

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

}

推荐答案

请尝试使用requestLocationUpdates代替getLastKnownLocation.您可以使用LocationListener侦听位置更新.更多信息: https://developer.android.com/reference/android/location/LocationListener.html

Instead of getLastKnownLocation, try requestLocationUpdates. You can use a LocationListener to listen for location updates. More info: https://developer.android.com/reference/android/location/LocationListener.html

这篇关于地图位置返回android的null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆