用于保存地图标记中的数据以在不同的活动android工作室中使用的选项

时间:2016-04-16 09:52:22

标签: android google-maps

我正在寻找有关选项的建议,我必须在Google地图上保存来自标记和来自placepicker的搜索数据。

我调查了共享首选项并尝试了代码但没有工作。

我需要从地方选择器搜索中获取地址数据并保存,以便历史记录活动可以生成列表。

地方选择器可以保存搜索吗?

我正在使用android studio

这是我的地方选择器的一个例子

    Place place = PlacePicker.getPlace(data, this);


            LatLng placeLatLng = place.getLatLng(); // gett lat lng from place
            double placeLat = placeLatLng.latitude;
            double placeLong = placeLatLng.longitude;
            final CharSequence name = place.getName();
            final CharSequence address = place.getAddress();
            Marker destination = mMap.addMarker(new MarkerOptions().position(new LatLng(placeLat, placeLong)).title("This is your destination"));

            LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            //Current Location
            Criteria criteria = new Criteria();
            String provider = locationManager.getBestProvider(criteria, true);
            Location myLocation = locationManager.getLastKnownLocation(provider);

            //Current Location LatLong
            final double currentLat = myLocation.getLatitude();
            final double currentLng = myLocation.getLongitude();


            List<CharSequence> listItems = new ArrayList<>();


            //Directions From Current Location To Destination
            final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?" + "saddr=" + currentLat + "," + currentLng + "&daddr=" + placeLat + "," + placeLong));
            intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
            listItems.add(name);
            listItems.add(address);
            startActivity(intent);
}
    }
    public void saveInfo(View v){
        SharedPreferences sharedPreferences = getSharedPreferences("Place Deatils", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
    }

1 个答案:

答案 0 :(得分:0)

使用Geocoder获取位置地址并将信息存储在SharedPreferences

   Geocoder geocoder;
            List<Address> addresses;
            geocoder = new Geocoder(this, Locale.getDefault());

            try {
                addresses = geocoder.getFromLocation(latt,lang, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
                String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
                String area = addresses.get(0).getSubLocality();
                String dist= addresses.get(0).getSubAdminArea();
                String city = addresses.get(0).getLocality();             
                mapMarker.setTitle(area+","+dist+","+city);
            } catch (IOException e) {
                e.printStackTrace();
            }

检查此SharedPreferences