-1

我想获得我所在位置的纬度和经度,之后,我将那里转换为位置。但我不知道我应该如何获得我所在位置的纬度和经度?

位置.java:

public class Location extends MapActivity {

MapView map;
MapController controller;
GeoPoint mygeo;
GeoPoint searchgeo;

@Override
public void onCreate(Bundle onSavedInstance){
    super.onCreate(onSavedInstance);
    setContentView(R.layout.location);

    map=(MapView)findViewById(R.id.mapView);
    LinearLayout zoomLayout=(LinearLayout)findViewById(R.id.zoom);
    View zoomView=map.getZoomControls();
    zoomLayout.addView(zoomView,new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,Gravity.BOTTOM+Gravity.CENTER_HORIZONTAL));

    map.displayZoomControls(true);
    //
    controller=map.getController();
    map.setSatellite(true);

    initMyLocation();


}
private void initMyLocation(){
    final MyLocationOverlay overlay=new MyLocationOverlay(this,map);
    overlay.enableMyLocation();
    overlay.enableCompass();
    overlay.runOnFirstFix(new Runnable(){
        public void run(){
            controller.setZoom(8);
            controller.animateTo(overlay.getMyLocation());
        }
    });
    map.getOverlays().add(overlay);

}


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

谢谢

干杯

4

4 回答 4

0

请按照以下步骤操作:

第 1 步:使用按钮创建如下所示的 main.xml。当你按下它应该为你当前的位置敬酒。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button
 android:id="@+id/retrieve_location_button"
 android:text="Retrieve Location"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 />
</LinearLayout>

第 2 步:创建一个 Activity,如下所示:

包 com.xxx.yyy.zzz;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class LbsGeocodingActivity extends Activity {

        private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
        private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds

        protected LocationManager locationManager;

        protected Button retrieveLocationButton;

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);

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

            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER,
                    MINIMUM_TIME_BETWEEN_UPDATES,
                    MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                    new MyLocationListener()
            );

        retrieveLocationButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    showCurrentLocation();
                }
        });       

        }   

        protected void showCurrentLocation() {

            Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            if (location != null) {
                String message = String.format(
                        "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                        location.getLongitude(), location.getLatitude()
                );
                Toast.makeText(LbsGeocodingActivity.this, message,
                        Toast.LENGTH_LONG).show();
            }

        }  

        private class MyLocationListener implements LocationListener {

            public void onLocationChanged(Location location) {
                String message = String.format(
                        "New Location \n Longitude: %1$s \n Latitude: %2$s",
                        location.getLongitude(), location.getLatitude()
                );
                Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
            }

            public void onStatusChanged(String s, int i, Bundle b) {
                Toast.makeText(LbsGeocodingActivity.this, "Provider status changed",
                        Toast.LENGTH_LONG).show();
            }

            public void onProviderDisabled(String s) {
                Toast.makeText(LbsGeocodingActivity.this,
                        "Provider disabled by the user. GPS turned off",
                        Toast.LENGTH_LONG).show();
            }

            public void onProviderEnabled(String s) {
                Toast.makeText(LbsGeocodingActivity.this,
                        "Provider enabled by the user. GPS turned on",
                        Toast.LENGTH_LONG).show();
            }

        }

    }

第 3 步:创建具有以下三个权限的 AndroidManifest.xml:

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

当您运行此应用程序时,您会在 android 设备的屏幕上找到一个按钮。当您按下此按钮时,您将看到 Toast 消息,其中包含您当前 gps 位置的纬度和经度。

于 2012-10-31T11:24:26.387 回答
0

看一下这个。它准确显示了如何获取您当前的位置,并在关闭并重新打开 Android 上的应用程序后获取其经纬度 GPS 不更新位置

请注意,您必须在清单中包含必要的权限才能使其正常工作

如此处所示https://stackoverflow.com/a/3380641/975959

于 2012-10-31T10:52:04.910 回答
0

使用位置监听器获取您当前的位置(纬度和经度)。

这将调用位置侦听器

    LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    LocationListener mlocListener = new MyLocationListener();  
    mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 

你可以在这堂课上得到你的经纬度

    public class MyLocationListener implements LocationListener

    {

    @Override 
    public void onLocationChanged(Location loc) 
    {        
     double longitude =  loc.getLongitude(); 
     double latitude = loc.getLatitude();  
    }  
    @Override 
    public void onProviderDisabled(String provider) 
    { 
       Toast.makeText( getApplicationContext(),"Enable GPS",Toast.LENGTH_SHORT ).show();
       Intent enablegps = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);    
       startActivity(enablegps);
    }

    @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) 
    {} }

希望能帮助到你。

于 2012-10-31T10:57:23.190 回答
0

特别是在您的情况下,请在 initMyLocation() 中执行此操作

double latitude = (overlay.getMyLocation().getLatitudeE6())/ 1e6;

double longitude = (overlay.getMyLocation().getLongitudeE6())/ 1e6;
于 2012-10-31T11:00:27.577 回答