2014-03-12 115 views
0

我想使用Google map Api在當前位置獲取標記。我在Android手機上進行了測試,我只能看到一張地圖。我目前的位置沒有標記。即使是我在代碼中放置的東西也沒有顯示。誰能幫我這個。 我張貼下面我的代碼:如何在Android中使用Google Maps Api獲取手機的當前位置?

public class MainActivity extends Activity implements LocationListener { 
GoogleMap map; 

@Override 
void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) 
.getMap(); 
} 

@Override 
    public void onLocationChanged(Location location) { 

map.clear(); 

MarkerOptions mp = new MarkerOptions(); 

    mp.position(new LatLng(location.getLatitude(), location.getLongitude())); 

    mp.title("my position"); 

    map.addMarker(mp); 
    Toast.makeText(getApplicationContext(), 
     location.getLatitude() +", "+location.getLongitude(), 
     Toast.LENGTH_LONG).show(); 

    map.animateCamera(CameraUpdateFactory.newLatLngZoom(
    new LatLng(location.getLatitude(), location.getLongitude()), 16)); 

    } } 
+0

添加所需的權限進入的manifest.xml –

+0

我已經添加的所有權限來體現。 – user3256145

回答

0

有一個在由谷歌地圖給出的,如果你想使用谷歌地圖獲得位置建造方法。

map.getLatitude(); 
map.getLongitude(); 

試試吧,它會起作用。

+0

它是googleMap.getLatitude()嗎? – user3256145

+0

=在你的情況下,它的「地圖」。 –

+0

我不能使用這些方法與地圖 – user3256145

0

您已經在此獲得位置:在您的移動GPS location.getLatitude(), location.getLongitude()

+0

但標記未設置到位置上 – user3256145

0

打開,到時將停止閃爍,則意味着它已得到了位置,然後標記將顯示。

1

剛剛從你的代碼中刪除map.clear();,並添加marker.remove();

這是爲我工作..嘗試這個,

protected void onCreate(Bundle savedInstanceState) 
      { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_main); 
       //actionBar.getActionBar(); 



       locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
       System.out.println("current "+locationManager); 

       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 120000, 1, this); 
       location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

       addGoogleMap(); 
       addMarkers(); 

      } 

      @TargetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressLint("NewApi") 
      private void addGoogleMap() { 
       if(googleMap == null){ 
       googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 
       googleMap.setOnMarkerClickListener(this); 
        googleMap.setOnMarkerDragListener(this); 
       } 

      } 


      private void addMarkers() { 

       if(googleMap != null) 
       { 
       lt = new LatLng(location.getLatitude(), location.getLongitude()); 


       marker = googleMap.addMarker(new MarkerOptions().position(lt) 
          .title("Current location ").snippet("Race Start: 9:00 AM CST") 
          .draggable(false)); 

       googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
          lt, 20)); 
       } 

      } 

@Override 
      public void onLocationChanged(Location location) { 
      //txtLat = (TextView) findViewById(R.id.textview1); 
      //txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude()); 
       System.out.println("loc changed"); 
       //googleMap.clear(); 
       marker.remove(); 
       locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
       System.out.println("current "+locationManager); 

       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 120000, 1, this); 
       location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
       lt = new LatLng(location.getLatitude(), location.getLongitude()); 
       marker = googleMap.addMarker(new MarkerOptions().position(lt) 
          .title("Current location ").snippet("Race Start: 9:00 AM CST") 
          .draggable(false)); 
} 
相關問題