Google Map v2: to display multiple locations on Google Maps

Steve

I need to point out five different locations on Google Maps. So far I got a output successfully. There was no errors in this code.

My only problem is it only points to a single location. It have to points to a different locations, for example, one have to point to Chennai and other have to point to Delhi or something else.

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

There was a five different marker you can see that in the coding.Every marker was point out to a same location.My only problem is, I need to show that each and every marker have to point out different location.

M D

Now try this way

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Google Map返回nullpointerexception Google Maps Android V2

来自分类Dev

Google Map返回nullpointerexception Google Maps Android V2

来自分类Dev

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

来自分类Dev

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

来自分类Dev

Google Maps Android API v2

来自分类Dev

使用Google Maps API v2

来自分类Dev

Google Android Maps v2 MetersToEquatorPixel

来自分类Dev

Android Google Maps v2测试

来自分类Dev

空白的Android Google Maps v2

来自分类Dev

Google Maps Android API v2

来自分类Dev

Android Google Maps v2导航

来自分类Dev

放大标记Google Map V2

来自分类Dev

Google Map V2异常NoClassDeffFound

来自分类Dev

Google Map v2 Heatmap人口

来自分类Dev

部署Google Maps v2 Google Play商店

来自分类Dev

使用Google Map v2无法显示Google Map

来自分类Dev

使用Google Map v2无法显示Google Map

来自分类Dev

Google Maps v2 not loading on some devices

来自分类Dev

更改OnMylocationButtonClick在Google Maps v2 Android中缩放

来自分类Dev

在Google Maps v2 Android上添加标记

来自分类Dev

使相机在Google Maps V2中居中

来自分类Dev

Google Maps Activity V2 Android Studio

来自分类Dev

在Android,Google Maps v2中绘制实时路线

来自分类Dev

Google Maps v2缩放到特定标记

来自分类Dev

Android Google Maps Api V2崩溃

来自分类Dev

Google Maps Android API v2停止工作

来自分类Dev

拖动折线onMarkerDrag Android Google Maps v2

来自分类Dev

Android Studio集成了Google Maps API v2

来自分类Dev

Google Maps v2 utils的ClusterManager重绘标记