2014-02-27 65 views
0

的多個位置,我在開發一個應用程序,我需要通過多個座標[緯度 和龍],顯示他們都在同一個地圖,我在我的地圖讓我的當前位置。但是,如何在地圖上獲得5個位置?獲取地圖的Android

我的代碼:

public class MainActivity extends FragmentActivity implements LocationListener { 
GoogleMap _googleMap; 
private static final LatLng GOLDEN_GATE_BRIDGE = 
     new LatLng(37.828891,-122.485884); 
LatLng myPosition; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    _googleMap = ((SupportMapFragment) 
      getSupportFragmentManager().findFragmentById(
      R.id.map)).getMap(); 
    if(_googleMap==null){ 
     Toast.makeText(getApplicationContext(), "Google Map Not Available", Toast.LENGTH_LONG).show(); 
     } 
     LocationManager locationManger = 
     (LocationManager)getSystemService(LOCATION_SERVICE); 
     Criteria criteria=new Criteria(); 
     String provider = locationManger.getBestProvider(criteria, true); 
     Location location = locationManger.getLastKnownLocation(provider); 
     if(location!=null){ 
      double latitude = location.getLatitude(); 
      double langitude = location.getLongitude(); 
      LatLng latlang = new LatLng(latitude, langitude); 
      LatLngBounds curScreen = 
       _googleMap.getProjection().getVisibleRegion().latLngBounds; 
      curScreen.contains(latlang); 
      myPosition = new LatLng(latitude, langitude); 
      Circle circle = _googleMap.addCircle(new CircleOptions() 
      .center(new LatLng(latitude, langitude)) 
      .radius(10000) 
      .strokeColor(Color.RED)); 
     _googleMap.moveCamera(CameraUpdateFactory.newLatLng(myPosition)); 
      _googleMap.addMarker(new 
      MarkerOptions().position(myPosition).title("start")); 


     } 

} 
+0

但是從哪裏得到的位置? –

+0

我想用我的當前位置帶五個位置。 – Durga

+0

我需要通過多個座標[緯度 和龍],顯示他們都在同一個地圖 – Durga

回答

3

這是嘗試非常簡單的創建一個Random Location按您Current Location像:

private double[] createRandLocation(double latitude, double longitude) { 
    return new double[] { latitude + ((Math.random() - 0.5)/500), 
      longitude + ((Math.random() - 0.5)/500), 
      150 + ((Math.random() - 0.5) * 10) }; 

} 

並添加到您的Map這樣的:

for (int i = 0; i < 10; i++) { 
     // random latitude and logitude 
     double[] randomLocation = createRandLocation(yourcurrentpositionLatitude, yourcurrentpositionlongitude); 

     // Adding a marker 
     MarkerOptions marker = new MarkerOptions().position(
       new LatLng(randomLocation[0], randomLocation[1])).title(
       "Hello Maps " + i); 


     System.out.println("latitude: " + randomLocation[0] + ", " 
       + randomLocation[1]); 

     Log.e("Random", "> " + randomLocation[0] + ", " + randomLocation[1]); 
     Toast.makeText(
       youractivity.this, 
       " Random Location " + randomLocation[0] + "," 
         + randomLocation[0], Toast.LENGTH_LONG).show(); 

     if (i == 0) 
      marker.icon(BitmapDescriptorFactory 
        .defaultMarker(BitmapDescriptorFactory.HUE_AZURE)); 
     if (i == 1) 
      marker.icon(BitmapDescriptorFactory 
        .defaultMarker(BitmapDescriptorFactory.HUE_BLUE)); 
     if (i == 2) 
      marker.icon(BitmapDescriptorFactory 
        .defaultMarker(BitmapDescriptorFactory.HUE_CYAN)); 
     if (i == 3) 
      marker.icon(BitmapDescriptorFactory 
        .defaultMarker(BitmapDescriptorFactory.HUE_GREEN)); 
     if (i == 4) 
      marker.icon(BitmapDescriptorFactory 
        .defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)); 
     if (i == 5) 
      marker.icon(BitmapDescriptorFactory 
        .defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)); 
     if (i == 6) 
      marker.icon(BitmapDescriptorFactory 
        .defaultMarker(BitmapDescriptorFactory.HUE_RED)); 
     if (i == 7) 
      marker.icon(BitmapDescriptorFactory 
        .defaultMarker(BitmapDescriptorFactory.HUE_ROSE)); 
     if (i == 8) 
      marker.icon(BitmapDescriptorFactory 
        .defaultMarker(BitmapDescriptorFactory.HUE_VIOLET)); 
     if (i == 9) 
      marker.icon(BitmapDescriptorFactory 
        .defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)); 


     // Add Marker belong here 
     mMap.addMarker(marker); 

    } 

輸出:圖片還包含一個Cirlce作爲中心創建了您的Current Location

enter image description here

+1

不錯的演示文稿。 –