1

剛剛熟悉Google Maps API(v2)並將其集成到我的Android應用程序中,並且我有兩個問題,但沒有找到最佳方法:Android應用程序中的Google地圖(V2)

首先這總是關注當前用戶的位置(如GPS應用程序)。我所做的是在OnCreate我得到的用戶位置(根據一些東西,我發現這裏和那裏))

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.map); 

     MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map); 

     LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 
     String bestProvider = locationManager.getBestProvider(criteria, true); 
     Location location = locationManager.getLastKnownLocation(bestProvider); 
     LatLng currentUserPosition = new LatLng(location.getLatitude(), location.getLongitude()); 
     // setting the cam position to current position 
     CameraPosition camPosition = new CameraPosition(currentUserPosition, 17f, 30f, 0f); 
     CameraUpdate update = CameraUpdateFactory.newCameraPosition(camPosition); 
     mapFragment.getMap().animateCamera(update); 

     // updating map every second 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
       1000, // 1-second interval. 
       10, // 10 meters. 
       listener); 
} 

哪裏listener是配置這樣的LocationListener

private final LocationListener listener = new LocationListener() { 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onLocationChanged(Location location) { 
     CameraPosition camPosition = new CameraPosition(new LatLng(location.getLatitude(), location.getLongitude()), 17f, 30f, 0f); 
     MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map); 
     CameraUpdate update = CameraUpdateFactory.newCameraPosition(camPosition); 
     mapFragment.getMap().animateCamera(update); 
    } 
}; 

是這個正確的方式來做到這一點?
第二件事是用戶可以觸摸地圖並在地圖上動態添加標記(使用他自己的標題和內容)。我試圖找到(但沒有)在標記之間繪製路徑並像gps應用程序一樣行事的方法。這甚至有可能嗎?
謝謝

回答

1

我強烈建議下從documentation開發者指南教程到地圖的Android API v2的集成到你的應用程序的正確方法,並檢查與谷歌Play服務SDK捆綁sample code

自動關注用戶(我的位置點)位置的最好(也是最簡單的)方法是實現OnMyLocationChangeListener並在該接口提供的回調中相應地更新相機。

要創建一系列連接標記的線段,您應該可以使用Polyline類。 編輯:您可能會發現這個相關answer對繪製兩個標記之間的路徑很有用。

0

那麼實際上在Google Map API V2可以使用:

map.setMyLocationEnabled(true); 

這將創建一個谷歌地圖窗口上的圖標按照用戶的當前位置。 其實我覺得這樣比較容易實現整個LocationListener

關於標記問題,您需要實現某種窗口,用戶可以在其中設置所有標記數據。在Activity

map.setOnMapClickListener(this); 
map.setOnMapLongClickListener(this); 

:然後你就可以動態地添加標記是這樣的:

marker = map.addMarker(new MarkerOptions().position(latlng).title(markersTitleArray[i]).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker))); 

,如果你想添加在地圖上的點擊這個標記,那麼你將需要實現。

最後關於你的最後一個問題,你已經對如何在地圖上繪製Polyline路提供我的回答:

Draw driving route between 2 GeoPoints on GoogleMap SupportMapFragment

相關問題