在地图Android上获取多个位置

时间:2014-02-27 12:07:07

标签: android google-maps

我正在开发一个应用程序,因为我需要传递多个坐标[Lat 和长]并在同一个地图上显示所有这些,我在我的地图上获取我当前的位置。但是如何在地图中获得5个位置?

我的代码:

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"));


        }

}

1 个答案:

答案 0 :(得分: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