2012-04-27 220 views
1

我想顯示設備在谷歌地圖中的當前位置我該怎麼做呢我現在正在顯示普通地圖。以下代碼顯示設備的緯度和高度位置如何使用該地圖在地圖中顯示地址和語言。如何顯示Android Google地圖中的當前位置?

package com.appulento.mapsexample.pack; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.Handler; 
import android.widget.Toast; 

import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapView; 

    public class MapsMianClass extends MapActivity { 
     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      MapView mapView = (MapView) findViewById(R.id.mapview1); 
      mapView.setBuiltInZoomControls(true); 



      /* 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) 
      { 

      } 

      } 

     @Override 
     protected boolean isRouteDisplayed() { 
      // TODO Auto-generated method stub 
      return false; 
     } 


     } 

在此先感謝.....

+1

爲什麼不只是看看這個頁面的右下角..並點擊**中給出的鏈接相關** – MKJParekh 2012-04-27 09:09:21

回答

0
  1. 採集GPS位置。
  2. 在地圖上繪製一個圖標(或一個標記)到GPS位置。
  3. 移動地圖,以便當前位置可見。

請告訴哪一步導致問題。

+0

我正面臨着將地圖移動到當前位置的問題,我正在獲取緯度和經度位置現在我編輯了我的最新代碼 – 2012-04-27 13:51:02

2

由於這個問題已經在這裏超過一年了,所以在Android上顯示地圖的方法已經改變。

獲得您的當前位置(lat &長)後,您只需將此座標傳遞給地圖標記。

下面是我複製&從Google Maps Android API V2文檔粘貼的片段:

private GoogleMap mMap; 
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 
mMap.addMarker(new MarkerOptions() 
     .position(new LatLng(0, 0)) 
     .title("Hello world")); 

至於如何使用谷歌地圖,他們也得到了文檔的完整指南。

我粘貼在這裏充分的步驟拯救你一條腿的鏈接:

Google Maps Android API V2 - Getting Started

如果你仔細按照相應的步驟,您應該顯示的地圖,並顯示標誌,沒有任何問題。

以下是您可能需要注意的一件事:在API控制檯中啓用Google地圖選項時,請確保您啓用了谷歌地圖Android API V2,並且請確保您'在檢索API密鑰時選擇了正確的證書。

希望這可以幫助你擺脫薄霧。

相關問題