在 Google Maps API v2 Android 中添加多个标记

Posted

技术标签:

【中文标题】在 Google Maps API v2 Android 中添加多个标记【英文标题】:Adding multiple markers in Google Maps API v2 Android 【发布时间】:2015-08-14 16:44:23 【问题描述】:

我想在我的地图中添加多个标记,但我不知道怎么做。

目前,我正在使用它,它可以正常工作:

Marker m1 = googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(38.609556, -1.139637))
                .anchor(0.5f, 0.5f)
                .title("Title1")
                .snippet("Snippet1")
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.logo1)));


Marker m2 = googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(40.4272414,-3.7020037))
                .anchor(0.5f, 0.5f)
                .title("Title2")
                .snippet("Snippet2")
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.logo2)));

Marker m3 = googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(43.2568193,-2.9225534))
                .anchor(0.5f, 0.5f)
                .title("Title3")
                .snippet("Snippet3")
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.logo3)));

但是当我想在我的地图中添加 300 个标记时,问题就来了。而且一件一件做起来很烦人。

有没有办法从数组或任何东西中读取标记?

另一个问题:我可以从外部文件中读取标记,这样我就可以在不接触应用代码的情况下添加或更新标记吗?

【问题讨论】:

您需要将您的LatLag 存储在一个arrayList 中,并使用for 循环添加多个标记。 【参考方案1】:
ArrayList<MarkerData> markersArray = new ArrayList<MarkerData>();

for(int i = 0 ; i < markersArray.size() ; i++) 

    createMarker(markersArray.get(i).getLatitude(), markersArray.get(i).getLongitude(), markersArray.get(i).getTitle(), markersArray.get(i).getSnippet(), markersArray.get(i).getIconResID());



protected Marker createMarker(double latitude, double longitude, String title, String snippet, int iconResID) 

    return googleMap.addMarker(new MarkerOptions()
            .position(new LatLng(latitude, longitude))
            .anchor(0.5f, 0.5f)
            .title(title)
            .snippet(snippet)
            .icon(BitmapDescriptorFactory.fromResource(iconResID)));

【讨论】:

欢迎来到 SO .. 请尝试写下您的解决方案。 不工作它只添加一个标记,即仅用于第一条记录【参考方案2】:

使用MarkerOptions

private GoogleMap googleMap;
private MarkerOptions options = new MarkerOptions();
private ArrayList<LatLng> latlngs = new ArrayList<>();

您可以添加到 latlngs 列表中,

 latlngs.add(new LatLng(12.334343, 33.43434)); //some latitude and logitude value

然后,使用 for 循环将它们设置在地图上。

 for (LatLng point : latlngs) 
     options.position(point);
     options.title("someTitle");
     options.snippet("someDesc");
     googleMap.addMarker(options);
 

【讨论】:

【参考方案3】:

所以,如果你从 txt 文件中获取坐标,你可以像这样读取它们:

BufferedReader reader = null;
try 
    reader = new BufferedReader(
        new InputStreamReader(getAssets().open("filename.txt"), "UTF-8")); 

    // do reading, usually loop until end of file reading 
    String mLine = reader.readLine();
    while (mLine != null) 
       //process line
       ...
       mLine = reader.readLine(); 
    
 catch (IOException e) 
    //log the exception
 finally 
    if (reader != null) 
         try 
             reader.close();
          catch (IOException e) 
             //log the exception
         
    

如果你的 txt 文件看起来像这样

23.45 43.23
23.41 43.65
.
.
.

你可以将字符串修改为 LatLng 对象:

String[] coord = mLine.split("\\r?\\n");
ArrayList<LatLng> coordinates = new ArrayList<LatLng>;

for(int i = 0; i <  coord.lenght(); ++i)
    String[] latlng =   coord.split(" ");
    coordinates.add(new LatLng(latlng[0], latlng[1]);

比:

for(LatLng cor : coordinates)
    map.addMarker(new MarkerOptions()
       .position(cor.getLat(), cor.getLng())
       .title("Hello"));

【讨论】:

我通过一个 txt 文件获取我的坐标,我在地图中存储了我需要的坐标【参考方案4】:

是的,您可以使用 ArrayList 将所有标记存储在该列表中,然后使用 for 循环在地图上添加标记。

例如:

googleMap.clear();
Now get all the marker in the Markers
//seachModelsList is the list of all markers
Marker[] allMarkers = new Marker[seachModelsList.size()];

for (int i = 0; i < seachModelsList.size(); i++)

    LatLng latLng = new LatLng(seachModelsList.get(i).getCoordinates()[1], seachModelsList.get(i)
            .getCoordinates()[0]);
    if (googleMap != null) 
        googleMap.setOnMarkerClickListener(this);
        allMarkers[i] = googleMap.addMarker(new MarkerOptions().position(latLng);
        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17.0f));
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17));

    

【讨论】:

【参考方案5】:

Kotlin 中,您可以这样做:-

让你拥有markerData列表作为markerList

val markerList = ArrayList<MarkerData>()

然后您可以通过 forEach loop 迭代列表并在 GoogleMap 中添加 Marker 为:-

 markerList.forEach markerData ->
     googleMap.addMarker(MarkerOptions()
    .position(LatLng(markerData.latitutde, markerData.longitude))
    .anchor(0.5f, 0.5f)
    .title(markerData.title)
    .snippet(markerData.snippet)
    .icon(BitmapDescriptorFactory.fromResource(markerData.iconResID)))
   

假设您的 MarkerData 是:-

class MarkerData(val latitutde : Double, val longitude : Double, val title : String, val snippets: String, @DrawableRes val iconResID: Int)

而您将 MarkerData 添加为 -:

MarkerData(
            35.61049,
            139.63007,
            "Tokyo",
            "hello Tokyo",
            R.drawable.ic_icon_user_review
        )

那么您必须为矢量资产图标创建一个自定义方法,如下所示:-

 private fun bitmapDescriptorFromVector(context: Context, vectorResId: Int): BitmapDescriptor? 
    return ContextCompat.getDrawable(context, vectorResId)?.run 
        setBounds(0, 0, intrinsicWidth, intrinsicHeight)
        val bitmap = Bitmap.createBitmap(intrinsicWidth, intrinsicHeight, Bitmap.Config.ARGB_8888)
        draw(Canvas(bitmap))
        BitmapDescriptorFactory.fromBitmap(bitmap)
    

然后对于 Marker,您的方法将是:

 markerList.forEach markerData ->
     googleMap.addMarker(MarkerOptions()
    .position(LatLng(markerData.latitutde, markerData.longitude))
    .anchor(0.5f, 0.5f)
    .title(markerData.title)
    .snippet(markerData.snippet)
    .icon(bitmapDescriptorFromVector(context!!, R.drawable.ic_icon_marker)))
   

【讨论】:

【参考方案6】:

这取决于您的数据来源。更好的方法是让你的自定义对象来存储数据。例如:

public class MyMarkerData 
        LatLng latLng;
        String title;
        Bitmap bitmap;

        public LatLng getLatLng() 
            return latLng;
        

        public void setLatLng(LatLng latLng) 
            this.latLng = latLng;
        

        public String getTitle() 
            return title;
        

        public void setTitle(String title) 
            this.title = title;
        

        public Bitmap getBitmap() 
            return bitmap;
        

        public void setBitmap(Bitmap bitmap) 
            this.bitmap = bitmap;
        
    

然后,您可以编写一些方法将数据从外部文件转换为自定义数据对象列表(但我认为这超出了这个问题的范围)。

然后只需将此数据传递给您的标记绘图方法并循环遍历它。将标记保存在一些数组列表或映射(对象,标记)中也是一个很好的做法,然后您可以轻松访问它。

类似的东西:

    HashMap<Marker, MyMarkerData> mDataMap = new HashMap<>();

        public void drawMarkers(ArrayList<MyMarkerData> data) 
                Marker m;
                for (MyMarkerData object: data) 
                    m = googleMap.addMarker(new MarkerOptions()
                            .position(object.getLatLng())
                            .title(object.getTitle())
                            .icon(BitmapDescriptorFactory.fromBitmap(object.getBitmap()));

                    mDataMap.put(m, object);
                
            

【讨论】:

那么,我应该在哪里添加数据? 这取决于你,但绝对是在调用draw方法之前。如果您使用的是外部文件,请尝试将其转换为 Java 对象 我也应该问一下如何通过这种方式添加数据,或者您可以举个例子。 类似:"38.609556, -1.139637, Title1, Snippet1, icon1" 那么您应该编写自己的解析器,它将读取您的文本文件并将此信息写入 Java 对象,或者考虑使用 JSON 或其他一些流行的格式,它们已经具有其解析机制。但无论如何这是一个全新的问题,我认为***上有足够的信息,请尝试谷歌它。

以上是关于在 Google Maps API v2 Android 中添加多个标记的主要内容,如果未能解决你的问题,请参考以下文章