从ArrayList获取字符串并转换为可用的纬度和经度

时间:2017-11-21 15:46:40

标签: java android google-maps arraylist

我一直在寻找答案的日子,但还没有运气,所以我想我会问。

我有一个小型的本地旅游应用程序,我一直在努力,我有大部分功能,但我无法弄清楚如何从我的ArrayList中的每个特定项目中提取纬度和经度信息

我现在拥有的,只有经度作为双倍,因为如果两者都是硬编码的地图应用程序正常工作,但我希望它根据用户选择的项目工作。

片段代码

public class RestaurantsFragment extends Fragment {

private double latitude;
private double longitude;

public RestaurantsFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.attraction_list, container, false);

    // Create a list of restaurants
    final ArrayList<Attractions> attraction = new ArrayList<Attractions>();
    attraction.add(new Attractions(R.string.black_n_blue, R.string.black_n_blue_contact, R.string.black_n_blue_desc, R.drawable.bnb, R.string.slater_loc));
    attraction.add(new Attractions(R.string.cardona, R.string.cardona_contact, R.string.cardona_desc, R.drawable.cardona, R.string.slater_loc));
    attraction.add(new Attractions(R.string.athos, R.string.athos_contact, R.string.athos_desc, R.drawable.athos, R.string.slater_loc));
    attraction.add(new Attractions(R.string.tanpopo, R.string.tanpopo_contact, R.string.tanpopo_desc, R.drawable.tanpopo, R.string.slater_loc));

    // Create an {@link AttractionsAdapter}, whose data source is a list of {@link Attractions}s. The
    // adapter knows how to create list items for each item in the list.
    AttractionsAdapter adapter = new AttractionsAdapter(getActivity(), attraction, R.color.restaurants);

    // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
    // There should be a {@link ListView} with the view ID called list, which is declared in the
    // attraction_list.xml layout file.
    ListView listView = (ListView) rootView.findViewById(R.id.list);

    // Make the {@link ListView} use the {@link AttractionsAdapter} we created above, so that the
    // {@link ListView} will display list items for each {@link Attractions} in the list.
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            // Get the {@link Attractions} object at the given position the user clicked on
            Attractions attractions = attraction.get(position);

            latitude = attractions.getLocation();
            longitude = -73.850338;

            //Open map app to the address
            String uri = String.format(Locale.ENGLISH, "geo:%f,%f", latitude, longitude);
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
            startActivity(intent);
        }
    });

    return rootView;
}

}

public class Attractions {
//Image reference source
private int mImageResourceId = NO_IMAGE_PROVIDED;
//Attraction name
private int mName;
//Attraction hours
private int mContact;
//Attraction description
private int mDescription;
//Attraction location
private int mLocation;

private static final int NO_IMAGE_PROVIDED = -1;

public Attractions(int name, int contact, int description, int imageResourceId, int location){
    mImageResourceId = imageResourceId;
    mName = name;
    mContact = contact;
    mDescription = description;
    mLocation = location;
}

public Attractions(int name, int contact, int description, int location){
    mName = name;
    mContact = contact;
    mDescription = description;
    mLocation = location;
}

public boolean hasImage() {
    return mImageResourceId != NO_IMAGE_PROVIDED;
}

public int getImageResourceId() {
    return mImageResourceId;
}

public int getName() {
    return mName;
}

public int getContact() {
    return mContact;
}

public int getDescription() {return mDescription;}

public int getLocation() {return mLocation;}

@Override
public String toString() {
    return "Word{" +
            "mImageResourceId=" + mImageResourceId +
            ", mName='" + mName + '\'' +
            ", mContact='" + mContact + '\'' +
            ", mDescription='" + mDescription + '\'' +
            ", mLocation='" + mLocation + '\'' +
            '}';
}
}

我只是将strings.xml格式化为:

<string name="slater_loc" formatted="false">42.689319</string> 

2 个答案:

答案 0 :(得分:0)

您是否尝试在/和double中存储/获取坐标? 无论如何,我会尝试制造两个建造者。一个用于构建地理坐标的StringBuilder和用于构建Uri的Uri构建器,然后检查活动是否可以解析,然后启动。

StringBuilder geoString = new StringBuilder();
geoString.append(lat)
    .append(",")
    .append(long);

Uri.Builder builder = new Uri.Builder();
builder.scheme("geo")
    .path(geoString);
Uri addressUri = builder.build();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(addressUri);
if(intent.resolveActivity(getPacketManager()) != null){
    startActivity(intent);
}

`

答案 1 :(得分:0)

根据Joachim Huet的建议,我尝试了parseDouble建议,该建议完全符合要求。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            // Get the {@link Attractions} object at the given position the user clicked on
            Attractions attractions = attraction.get(position);

            String lat = getString(attractions.getLatitude());
            latitude = Double.parseDouble(lat);
            String lon = getString(attractions.getLongitude());
            longitude = Double.parseDouble(lon);

            //Open map app to the address
            String uri = String.format(Locale.ENGLISH, "geo:%f,%f", latitude, longitude);
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
            startActivity(intent);
        }
    });

此代码已添加到Restaurants Fragment中。谢谢大家的帮助!