在android中通过gps显示当前位置

在android中通过gps显示当前位置,android,google-maps,gps,Android,Google Maps,Gps,我正在开发一个应用程序,它应该在Android上的谷歌地图中显示设备的当前位置。问题是我没有得到设备的准确或接近位置 我的课程是: public class GPSLocatorActivity extends MapActivity { private MapView mapView; private MapController mapController; private LocationManager locationManager; private Lo

我正在开发一个应用程序,它应该在Android上的
谷歌地图
中显示设备的当前位置。问题是我没有得到设备的准确或接近位置

我的课程是:

public class GPSLocatorActivity extends MapActivity {
    private MapView mapView;
    private MapController mapController;

    private LocationManager locationManager;
    private LocationListener locationListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    

        locationListener = new GPSLocationListener();

        locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            0, 
            0, 
            locationListener);

        mapView = (MapView) findViewById(R.id.mapView);

        // enable Street view by default
//        mapView.setStreetView(true);

        // enable to show Satellite view
        mapView.setSatellite(true);

        // enable to show Traffic on map
         mapView.setTraffic(true);
        mapView.setBuiltInZoomControls(true);

//        myLocationOverlay=new MyLocationOverlay(this,mapView);
        mapController = mapView.getController();
        mapController.setZoom(16); 
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    private class GPSLocationListener implements LocationListener 
    {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                GeoPoint point = new GeoPoint(
                        (int) (location.getLatitude() * 1E6), 
                        (int) (location.getLongitude() * 1E6));

                /* Toast.makeText(getBaseContext(), 
                        "Latitude: " + location.getLatitude() + 
                        " Longitude: " + location.getLongitude(), 
                        Toast.LENGTH_SHORT).show();*/

                mapController.animateTo(point);
                mapController.setZoom(16);

                // add marker
                MapOverlay mapOverlay = new MapOverlay();
                mapOverlay.setPointToDraw(point);
                List<Overlay> listOfOverlays = mapView.getOverlays();
                listOfOverlays.clear();
                listOfOverlays.add(mapOverlay);

                String address = ConvertPointToLocation(point);
                Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();

                mapView.invalidate();
            }
        }

        public String ConvertPointToLocation(GeoPoint point) {   
            String address = "";
            Geocoder geoCoder = new Geocoder(
                    getBaseContext(), Locale.getDefault());
            try {
                List<Address> addresses = geoCoder.getFromLocation(
                    point.getLatitudeE6()  / 1E6, 
                    point.getLongitudeE6() / 1E6, 1);

                if (addresses.size() > 0) {
                    for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
                        address += addresses.get(0).getAddressLine(index) + " ";
                }
            }
            catch (IOException e) {                
                e.printStackTrace();
            }   

            return address;
        } 

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }

    class MapOverlay extends Overlay
    {
        private GeoPoint pointToDraw;

        public void setPointToDraw(GeoPoint point) {
            pointToDraw = point;
        }

        public GeoPoint getPointToDraw() {
            return pointToDraw;
        }

        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
            super.draw(canvas, mapView, shadow);                   

            // convert point to pixels
            Point screenPts = new Point();


  mapView.getProjection().toPixels(pointToDraw, screenPts);

        // add marker
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.red);
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null); // 24 is the height of image        
        return true;
    }
    } 
}
公共类GPSLocatorActivity扩展了MapActivity{
私有地图视图;
专用地图控制器;
私人场所经理场所经理;
私有位置侦听器位置侦听器;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationManager=(locationManager)getSystemService(Context.LOCATION\u服务);
locationListener=新的GPSLocationListener();
locationManager.RequestLocationUpdate(
LocationManager.GPS\u提供程序,
0, 
0, 
位置(监听器);
mapView=(mapView)findViewById(R.id.mapView);
//默认情况下启用街景
//mapView.setStreetView(真);
//启用以显示卫星视图
mapView.setSatellite(真);
//启用以在地图上显示流量
mapView.setTraffic(true);
mapView.SetBuilTinZoomControl(真);
//myLocationOverlay=新建myLocationOverlay(此为地图视图);
mapController=mapView.getController();
mapController.setZoom(16);
}
@凌驾
受保护的布尔值isRouteDisplayed(){
返回false;
}
私有类GPSLocationListener实现LocationListener
{
@凌驾
已更改位置上的公共无效(位置){
如果(位置!=null){
地质点=新的地质点(
(int)(location.getLatitude()*1E6),
(int)(location.getLongitude()*1E6));
/*Toast.makeText(getBaseContext(),
“纬度:”+位置。getLatitude()+
“经度:”+location.getLongitude(),
吐司。长度(短)。show()*/
mapController.animateTo(点);
mapController.setZoom(16);
//添加标记
MapOverlay MapOverlay=新的MapOverlay();
mapOverlay.setPointToDraw(点);
List ListoForLays=mapView.getOverlays();
listOfOverlays.clear();
添加(映射覆盖);
字符串地址=转换点位置(点);
Toast.makeText(getBaseContext(),地址,Toast.LENGTH_SHORT).show();
mapView.invalidate();
}
}
公共字符串转换点定位(地理点){
字符串地址=”;
地理编码器地理编码器=新地理编码器(
getBaseContext(),Locale.getDefault();
试一试{
列表地址=geoCoder.getFromLocation(
point.getLatitudeE6()/1E6,
point.getLongitudeE6()/1E6,1);
如果(地址.size()>0){
for(int index=0;index
我在Mainfest文件中添加了权限:

 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />    
    <uses-permission android:name="android.permission.INTERNET" />


我已经添加了权限,并对地图使用了精确的Google Api密钥。

您是否尝试过这样做

使用此选项可以获取当前位置的纬度和经度。然后传递值以获取地图

   public class MyLocationListener implements LocationListener

{

@Override

public void onLocationChanged(Location loc)

{

loc.getLatitude();

loc.getLongitude();

String Text = “My current location is: “ +

“Latitud = “ + loc.getLatitude() +

“Longitud = “ + loc.getLongitude();

Toast.makeText( getApplicationContext(),

Text,

Toast.LENGTH_SHORT).show();

}

@Override

public void onProviderDisabled(String provider)

{

Toast.makeText( getApplicationContext(),

“Gps Disabled”,

Toast.LENGTH_SHORT ).show();

}

@Override

public void onProviderEnabled(String provider)

{

Toast.makeText( getApplicationContext(),

“Gps Enabled”,

Toast.LENGTH_SHORT).show();

}

@Override

public void onStatusChanged(String provider, int status, Bundle extras)

{

}

}

}
示例链接


我尝试了这两个例子……但在我的例子中,我希望用户通过Gps或网络获得当前位置。那是在我启动地图时……地图应显示用户当前位置……我尝试了许多教程,但无法获得用户当前位置。Plz Plz Plz Plz help me…..这里我为您编辑了检查..希望它能帮助您..否则请使用dropbox将完整代码发送给我..我会尝试..非常感谢@janmejoy..非常感谢..非常感谢..我现在无法找到我的位置,非常感谢您.再次非常感谢...:()))您使用的是Android map apiv2吗..我正在使用“Google Maps Android v1 API Key”这是您实际得到的结果。在结果中,我得到的地图总是以“Tulsa”位置启动。它没有显示我当前的位置。它没有收到实际的纬度和经度..对。。