我想通过使用“位置名称”是“国家/地区名称”来将MapView设置为位置。我得到异常: 通过在MapView中使用位置名称动画到位置-Android
如果你不能看到那么PIC的例外是:: IOException异常:服务不可用错误无法获取连接工厂客户。
我使用的代码如下:
public class ShowMapView extends MapActivity
{
MapView mapView;
MapController mapController;
String Country;
@Override
public void onCreate(Bundle bundleMap)
{
super.onCreate(bundleMap);
setContentView(R.layout.show_map);
mapView = (MapView)findViewById(R.id.MapView);
LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView,
new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mapView.setBuiltInZoomControls(true);
Intent intent = this.getIntent();
Country = intent.getStringExtra("Country");
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
Log.v("GEOCODER", geocoder.toString());
try
{
Log.v("MAP", "TRY BLOCK");
List<Address> address = geocoder.getFromLocationName(Country, 2);
Log.v("MAP", "Country: " + Country);
if(address.size() > 0)
{
Log.v("MAP", "GeoPoint p");
GeoPoint p = new GeoPoint((int)(address.get(0).getLatitude() * 1E6),
(int)(address.get(0).getLongitude() * 1E6));
Log.v("MAP", "L&L done");
mapController.animateTo(p); Log.v("MAP", "Animate to P");
mapView.invalidate(); Log.v("MAP", "Invalidate()");
}
}
catch(IOException e)
{
Log.v("MAP", "CATCH BLOCK");
e.printStackTrace();
}
}
@Override
protected boolean isRouteDisplayed()
{
Log.v("MAP", "ROUTE: false");
return false;
}
}
清单文件是如下::
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.project.LawSource"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<uses-library android:name="com.google.android.maps"/>
<activity
android:name=".GetCountry"
android:label="@string/app_label" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="ShowMapView"
android:label="@string/app_name">
</activity>
</application>
</manifest>
我照着this tutorial工作,但它不能正常工作。
让我知道如果我失去了一些东西。
我得到的问题,相同this blog
希望有一个解决方案。
在此先感谢,
Haps。
感谢您的回复。 此代码给出了这个异常,但也没有帮助我到达期望的位置。 – Harpreet 2012-03-05 10:47:44
我写了一个非常简单的地理编码器测试。我注意到你实际上并没有设置你的mapController。在mapView被分配后,你应该设置mapController = mapView.getController();我有一种感觉是一个问题,加上我怀疑你的getStringExtra对intent对象的调用可能返回null。另外,你的国家变量应该是小写的。我发布了我非常简单的例子。 – 2012-03-05 11:33:53
感谢您的回复,我根据您的调整了我的代码,但异常仍然是相同的,它执行代码到TRY BLOCK并执行“列表
address = geocoder.getFromLocationName(country,2)”时执行CATCH Block。 “因此该行不执行,现在它也停止显示之前显示的地图。 – Harpreet 2012-03-06 04:41:38