Adding multiple markers in Google Maps API v2 Android

前端 未结 6 776
礼貌的吻别
礼貌的吻别 2020-11-30 02:23

I want to add multiple markers in my map, but I don\'t know the way.

At the moment, I\'m using this, and it works correctly:

Marker m1 = googleMap.ad         


        
6条回答
  •  情话喂你
    2020-11-30 02:36

    So, if you get coordinates from txt file you could read them like this:

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

    if your txt file looks like this

    23.45 43.23
    23.41 43.65
    .
    .
    .
    

    Than you could modify string to LatLng objects:

    String[] coord = mLine.split("\\r?\\n");
    ArrayList coordinates = new ArrayList;
    
    for(int i = 0; i <  coord.lenght(); ++i){
        String[] latlng =   coord.split(" ");
        coordinates.add(new LatLng(latlng[0], latlng[1]);
    }
    

    And than:

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

提交回复
热议问题