簡體   English   中英

獲取用戶當前位置並在Google地圖中確定位置

[英]Get user current location and determine it in Google maps

我想問你:我在應用程序中使用Google Maps,當我打開包含地圖的活動時,我希望確定用戶的當前位置,因此我正在使用此代碼來檢索當前的用戶位置

         public void locat ()
        {
       //Use the LocationManager class to obtain GPS locations 

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

        LocationListener mlocListener = new MyLocationListener();


        mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);


        }


        // Class My Location Listener 

        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)

        {


        }

        }// End of Class MyLocationListener */

但這只是給我緯度和經度,而這不是我想要的! 我想確定用戶在Google地圖中的當前位置!我該怎么辦? 您需要我的代碼才能進入Google地圖嗎? 請幫幫我! 謝謝

如果我正確理解了您的問題,則希望獲取用戶當前的位置並將其顯示在地圖上。 您具有經度和緯度值,您要做的就是構造一個GeoPoint並使其與地圖上的該位置相關聯。 如果需要,您還可以在地圖上添加自定義疊加層。 類似以下內容應有所幫助。

double lat = location.getLatitude();
double longi = location.getLongitude();
long time = location.getTime();
GeoPoint currentGeo = new GeoPoint(toMicroDegrees(lat), toMicroDegrees(longi));
MapController controller = map.getController();
controller.animateTo(currentGeo);

您可以使用geo uri打開Goog​​le地圖,如下所示:

    String url = "geo:48.200927,16.369548,192";
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);

編輯:看來您想要反向地理編碼。 這是一個例子:

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);

這是android Geocoder類的文檔-http: //developer.android.com/reference/android/location/Geocoder.html

您實際上從未將信息傳遞給地圖以顯示位置。

您需要做的是從顯示的布局中獲取MapView。

然后獲取地圖視圖的控制器,並將用戶位置設置為其位置的中心/動畫。

類似於以下內容:

mapView1 = (MapView) this.findViewById(R.id.map);
MapController mapControl = mapView1.getController();
GeoPoint geo = new GeoPoint((int)(lat)*1e6), (int(long*1e6));
mapControl.animateTo(geo);

該鏈接應該可以幫助http://mobiforge.com/developing/story/using-google-maps-android

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM