2016-05-16 37 views
-1

你好,我是完全新的Android位置概念,我已經在android studio中創建了一個地圖活動。這是代碼。獲取當前位置Android API級別23

public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 

     mMap.setMyLocationEnabled(true); 

     Location location = null; 
     LatLng currentPosition = new LatLng(location.getLatitude(), location.getLongitude()); 
     mMap.addMarker(new MarkerOptions().position(currentPosition).title("Current Location")); 

這是正確的@Abhishek

+0

只需添加下列行:mMap.setMyLocationEnabled(真); – nikk

+0

似乎它已經在這篇文章中回答http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-on- a?rq = 1 –

+0

http://ramsandroid4all.blogspot.in/2015/05/showing-current-location-with-marker-in.html –

回答

1

您可以使用這樣

@Override 
public void onMapReady(GoogleMap map) { 
    // TODO Auto-generated method stub 
    map.setMyLocationEnabled(true); 
    LatLng currentPosition = new LatLng(location.getLatitude(),location.getLongitude()); 
    map.addMarker(new MarkerOptions().position(currentPosition).title("Current Location")); 
} 

locationonLocationChanged方法

@Override 
public void onLocationChanged(Location loc) { 
// TODO Auto-generated method stub 
location = new LatLng(loc.getLatitude(), loc.getLongitude()); 

---------- 

} 
+0

這是給錯誤,電話需要許可? –

+0

感謝它的工作,但問題是它是不可見的標記,你能幫我解決,如何使用標記我的位置.. –

+0

什麼是那個位置,從哪裏得到的?你在哪裏初始化「位置」.. @Abhishek –

0

的谷歌地圖API的位置現在工作搞定,甚至有聽衆,你可以用它做,例如充足:

private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() { 
    @Override 
    public void onMyLocationChange(Location location) { 
     LatLng loc = new LatLng(location.getLatitude(), location.getLongitude()); 
     mMarker = mMap.addMarker(new MarkerOptions().position(loc)); 
     if(mMap != null){ 
      mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f)); 
     } 
    } 
}; 

,然後設置地圖監聽器:

mMap.setOnMyLocationChangeListener(myLocationChangeListener);