0

我有一个纬度和经度的数组列表。我想使用这些列表在谷歌地图 api v2 中显示多个标记。但我在地图上看不到任何标记。你能帮帮我吗?

我的代码在这里..

package com.example.goomapapp;
import java.util.ArrayList;
import java.util.Arrays;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;




public class Atmactivity extends FragmentActivity {

     private GoogleMap googleMap=null;
      ArrayList<PositionClass>position;
      PositionClass data;
     ArrayList<Double>longitude=new ArrayList<Double>(Arrays.asList
             (23.882071,
             23.757696,
             23.782203,
             23.799481,
             23.800738,
             23.728155));
     ArrayList<Double>latitude=new ArrayList<Double>(Arrays.asList
             (90.398834,90.393051,90.365585,90.41571,90.42189,90.41394));
     ArrayList<String>Title=new ArrayList<String>(Arrays.asList
             ("tonghi","Farmgate","Agargao","gulshan2","baridhara","Ramna"));
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_atmactivity);
            position=new ArrayList<PositionClass>();
            for(int i=0;i<longitude.size();i++)
            {
            data=new PositionClass();
            data.setLatitude(latitude.get(i));
            data.setLongitude(longitude.get(i));
            data.setTitle(Title.get(i));
            position.add(data);
            }


            try {
                // Loading map
                initilizeMap();

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

        }

        /**
         * function to load map. If map is not created it will create it for you
         * */
        private void initilizeMap() {
            if (googleMap == null) {


                googleMap=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
                googleMap.getUiSettings().setZoomControlsEnabled(false);
                for(int i=0;i<position.size();i++)
                {
                     googleMap.addMarker(new MarkerOptions().title(position.get(i).Title).
                            position(new LatLng(position.get(i).latitude,position.get(i).longitude)).title("hello")
                            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

                }

                googleMap.setMyLocationEnabled(true);
                 if (googleMap == null) {
                    Toast.makeText(getApplicationContext(),
                            "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                            .show();
                }
                }
        }

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

}

职位类别是..

package com.example.goomapapp;

public class PositionClass {
    public double longitude;
    public double latitude;
    public String Title; 
    public void setLatitude(double val)
    {
        this.latitude=val;
    }

    public void setLongitude(double val)
    {
        this.longitude=val;
    }

    public void setTitle(String text)
    {
        this.Title=text;
    }

}
4

1 回答 1

0

尝试把这部分:

googleMap.addMarker(new MarkerOptions().title(position.get(i).Title).
                        position(new LatLng(position.get(i).latitude,position.get(i).longitude)).title("hello")
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

使用(from class) 方法Runnable将其执行到 UI 线程中。您的代码将如下所示:runOnUiThread(Runnable r)Activity

runOnUiThread(new Runnable () {
        @Override
        public void run() {
            aux = map.addMarker(new MarkerOptions()
                    .position(coord)
                    .title(title)); 
        } 
   });
于 2013-08-27T11:29:43.413 回答