Google Map v2:在Google地图上显示多个位置

时间:2014-10-13 06:40:10

标签: android google-maps-android-api-2

我需要在Google地图上指出五个不同的位置。到目前为止,我成功地获得了输出。此代码中没有错误。

我唯一的问题是它只指向一个位置。它必须指向不同的位置,例如,一个必须指向金奈,其他必须指向德里或其他。

MainActivity.java:

  public class MainActivity extends Activity {

    // Google Map
    private GoogleMap googleMap;

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

        try {
            // Loading map
            initilizeMap();

               googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

            double latitude[] ={13.07891,13.11602,13.11355,13.12511,13.08367};
            double longitude[] = {80.28215,80.23166,80.29613,80.29554,80.23961};

            // lets place some 10 random markers
            for (int i = 0; i < 5; i++) {
                // random latitude and logitude
                double[] randomLocation = createRandLocation(latitude[i],
                        longitude[i]);

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

                Log.e("Random", "> " + randomLocation[0] + ", "
                        + randomLocation[1]);

                // changing marker color
                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));

                googleMap.addMarker(marker);

                // Move the camera to last position with a zoom level
                if (i == 4) {
                    CameraPosition cameraPosition = new CameraPosition.Builder()
                            .target(new LatLng(randomLocation[0],
                                    randomLocation[1])).zoom(15).build();

                    googleMap.animateCamera(CameraUpdateFactory
                            .newCameraPosition(cameraPosition));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }

    /**
     * function to load map If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    /*
     * creating random postion around a location for testing purpose only
     */
    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) };
    }
} 

在编码中你可以看到五种不同的标记。每个标记都指向同一个位置。我唯一的问题是,我需要证明每个标记都必须指出不同的位置。

2 个答案:

答案 0 :(得分:2)

现在尝试这种方式

     double latitude[] ={13.07891,13.11602,13.11355,13.12511,13.08367};
     double longitude[] = {80.28215,80.23166,80.29613,80.29554,80.23961};

  // lets place some 5 markers
        for (int i = 0; i < 5; i++) {

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

            // changing marker color
            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));

            googleMap.addMarker(marker);

            // Move the camera to last position with a zoom level
            if (i == 4) {
                CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(new LatLng(randomLocation[0],
                                randomLocation[1])).zoom(15).build();

                googleMap.animateCamera(CameraUpdateFactory
                        .newCameraPosition(cameraPosition));
            }
        }

答案 1 :(得分:0)

您可以使用循环并在每次迭代和显示中创建新标记。请看一下类似的实现

for (int i=0;i<yourarray.length();i++) {
                Marker marker = userMap.addMarker(new MarkerOptions()
                        .position(lat[i], long[i])
                        .title("TITLE");
            }