2016-12-17 29 views
-2

我真的很感谢这个问题的一些帮助。我已经尝试了很多不同的解决方案,我在这里找到了它们,而且它们都不适合我。我在android studio上的应用中有Google地图活动。目前,我的用户可以点击地图来放置引脚。代码如下。在Android工作室创建一个数组与谷歌地图API来存储标记

mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() { 

    @Override 
    public void onMapClick(LatLng latLng) { 

    // Creating a marker 
    MarkerOptions markerOptions = new MarkerOptions(); 

    // Setting the position for the marker 
    markerOptions.position(latLng); 

    // Setting the title for the marker. 
    // This will be displayed on taping the marker 
    markerOptions.title(latLng.latitude + " : " + latLng.longitude); 

    // Animating to the touched position 
    mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng)); 

    // Placing a marker on the touched position 
    mMap.addMarker(markerOptions); 

     } 
    }); 

我需要的是经纬度和每个标记被丢弃保存到一个数组。用户可以根据需要放置尽可能多的引脚。我试图用for循环实现这一点,但我没有运气。如果有人可以告诉我如何设置阵列并将所有信息保存到其中,将不胜感激。

问候Peakapot

+0

声明一个'私有列表标记=新的ArrayList <>();',并添加'Marker's给它做'markers.add(MMAP。 addMarker(markerOptions));' – antonio

回答

0

我在项目中,我存储标记纬度,经度,标题共享偏好,并从储值加载它们来实现这样的功能。

声明变量

SharedPreferences sharedPreferences; 
int locationCount = 0; 

初始化sharedPreferences:

sharedPreferences = getSharedPreferences("location", 0); 

现在用这个方法来存储标记值:

public void Save_marker(Marker m) { 
    //pass market object as parameter 
    ++locationCount; 
    /** Opening the editor object to write data to sharedPreferences */ 
    SharedPreferences.Editor editor = sharedPreferences.edit(); 

    // Storing the latitude for the i-th location 
    editor.putString("lat" + Integer.toString((locationCount - 1)), Double.toString(m.getPosition().latitude)); 

    // Storing the longitude for the i-th location 
    editor.putString("lng" + Integer.toString((locationCount - 1)), Double.toString(m.getPosition().longitude)); 

    // Storing the count of locations or marker count 
    editor.putInt("locationCount", locationCount); 
    editor.putString("title", m.getTitle()); 
    /** Saving the values stored in the shared preferences */ 
    editor.commit(); 
} 

现在用于获取存储的标记值可以使用:

public void Get_markers() { 
    String title_name = ""; 
    Integer image_icon = R.drawable.currentlocation_icon; //marker icon for map 
    sharedPreferences = getSharedPreferences("location", 0); 
    // Getting number of locations already stored 
    locationCount = sharedPreferences.getInt("locationCount", 0); 
    String[] location_name = new String[locationCount]; 
    if (locationCount != 0) { 

     String lat = ""; 
     String lng = ""; 

     // Iterating through all the locations stored 
     for (int i = 0; i < locationCount; i++) { 
      // Getting the latitude of the i-th location 
      lat = sharedPreferences.getString("lat" + i, "0"); 
      title_name = sharedPreferences.getString("title", ""); 
      // Getting the longitude of the i-th location 
      lng = sharedPreferences.getString("lng" + i, "0"); 
      location_name[i] = title_name; 
      // Drawing marker on the map 
      drawMarker(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng)), title_name); 
     } 


private void drawMarker(LatLng point, String title_marker) { 
    // Creating an instance of MarkerOptions 
    MarkerOptions markerOptions = new MarkerOptions(); 
    // Setting latitude and longitude for the marker 
    markerOptions.position(point); 
    // Adding marker on the Google Map 
    mMap.addMarker(markerOptions); 
    //here mMap is your google map object 
} 

全面落实看到此链接: http://www.androidtrainee.com/adding-multiple-marker-locations-in-google-maps-android-api-v2-and-save-it-in-shared-preferences/

+0

非常感谢。 – Peakapot

+0

@Peakapot如果它的工作..不要忘记接受并upvote答案..问题 – rafsanahmad007

+0

我已upvoted的答案,但因为我的低声誉它没有出现。我需要先获得15以上。当人们只是简单地问我一个问题而投我票时,这将会非常困难。再次感谢您花时间帮助我。赞赏。 – Peakapot