我正在尝试创建最多6行地名和LatLng数组,然后使用意图将其发送到用户可以选择名称的地图类。该名称仍然需要与LatLng绑定,以便地图可以居中并缩放。
我在下面添加了已编辑的代码。我有putExtra和putParcelable的混合,因为我正在尝试任何东西来获得一个工作原型。希望它有一定道理。
发送类地理编码是这样的:
private void geocodeLocation(String gPlace) {
Geocoder gGeocoder = new Geocoder(this, Locale.getDefault());
try{
List<Address> results = null;
if(Geocoder.isPresent()){
results = gGeocoder.getFromLocationName(gPlace, numberOptions);
} else {
return;
}
Iterator<Address> locations = results.iterator();
String country;
int opCount = 0;
while(locations.hasNext()){
Address location = locations.next();
raw += location+"\n";
strAddressOutput[opCount] = location.getAddressLine(0) + ", " + location.getAddressLine(1) + country + "\n";
strLatOutput[opCount] = location.getLatitude();
strLongOutput[opCount] = location.getLongitude();
gLatLng = new LatLng(location.getLatitude(),location.getLongitude());
strLatLng[opCount] = gLatLng;
opCount ++;
}
for(int i = 0; i<opCount; i++){
}
} catch (IOException e){
}
发送类意图是这样的:
public void onConnected(Bundle bundle) {
if (!android.location.Geocoder.isPresent()) {
return;
}
if (gAddressRequested) {
geocodeLocation(gPlace);
Intent tdMap = new Intent(this, com.dm4_map_test.todos3maps.MainMap.class);
tdMap.putExtra(Constants.RETURNEDADDRESS, strAddressOutput);
Bundle args = new Bundle();
args.putString(Constants.PLACE, gPlace);
args.putParcelable(Constants.MAPCENTRE, gMapCentre);
args.putParcelableArray(Constants.LATLNGS, strLatLng);
tdMap.putExtra("bundle", args);
startActivity(tdMap);
}
}
接收类是这样的:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_map);
MapFragment mapFrag = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
if (savedInstanceState == null) {
needsInit = true;
}
mapFrag.getMapAsync(this);
mPlace = (EditText) findViewById(R.id.txAddress);
mAddressList = (Spinner) findViewById(R.id.spinAddressList);
mPlace.setText(getIntent().getStringExtra(Constants.PLACE));
String[] recAddressAA = getIntent().getStringArrayExtra(Constants.RETURNEDADDRESS); // NULLS from Geocoder?
recAddressOutput = recAddressAA;
String testAddressOutput = Arrays.toString(recAddressAA);
Bundle bundleMapCentre = getIntent().getParcelableExtra("bundle");
mMapCentre = bundleMapCentre.getParcelable (Constants.MAPCENTRE);
Bundle bundleLatLng = getIntent().getParcelableExtra("bundle");
LatLng[] mLatLng = bundleLatLng.getParcelableArrayList(Constants.LATLNGS);
mAdapter = new ArrayAdapter<String>(MainMap.this, android.R.layout.simple_spinner_item, recAddressOutput);
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mAddressList.setAdapter(mAdapter);
});
}
'LatLng [] mLatLng = bundleLatLng.getParcelableArrayList(Constants.LATLNGS);'错误,但我已退后一步,看看这是否是一个可行的方法