我该如何找到我所在位置的纬度和经度?

时间:2012-10-31 10:47:10

标签: android google-maps latitude-longitude

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

Location.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 个答案:

答案 0 :(得分:0)

检查一下。它显示了如何获取当前位置,并获得其纬度和长度 GPS not update location after close and reopen app on android

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

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

答案 1 :(得分: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) 
    {} }

希望它有所帮助。

答案 2 :(得分:0)

特别是在你的情况下,在initMyLocation()

中执行此操作
double latitude = (overlay.getMyLocation().getLatitudeE6())/ 1e6;

double longitude = (overlay.getMyLocation().getLongitudeE6())/ 1e6;

答案 3 :(得分: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步:创建如下的活动:

package 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位置的纬度和经度。

相关问题