2013-02-07 25 views
1

我可以顯示地圖並在地圖上顯示按鈕,當我點擊它時,它完全符合我的要求。在我的代碼,我有這樣的:如何獲取用戶位置並放大加載地圖Android V2

googleMap.setMyLocationEnabled(true); 
googleMap.getMyLocation(); 

是否有辦法應在加載地圖自動執行此操作,而不必挖掘放大到位置按鈕?

我研究了這個,找到了獲取用戶位置的例子,但是我發現的例子沒有做我想要的,我不確定它是否是V2地圖的代碼。現在肯定有一個簡單的代碼最簡單的方法來做到這一點?

我通過This後#2閱讀,但我不知道如何處理這樣的代碼:

googleMap.getCameraPosition().target 

目標需要我認爲一個位置?

任何幫助表示讚賞!

謝謝!

回答

2

我解決了這個使用下面的代碼:

lm =(LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    Criteria crit = new Criteria(); 

    towers = lm.getBestProvider(crit, false); 

    Location location = lm.getLastKnownLocation(towers); 

    if(location != null){ 

     Double glat = location.getLatitude(); 
     Double glon = location.getLongitude(); 

    } 

然後設置地圖了:

CameraPosition cp = new CameraPosition.Builder() 
     .target(new LatLng(glat, glon)) 
     .zoom(15) 
     .build();  
     googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cp)); 

這得到了用戶的位置,並在地圖加載,它會自動放大成地點,你應該在哪裏看到一個藍色的圓點!

相關問題