Android Can';t在应用程序上显示谷歌地图视图
首先,你必须知道英语不是我的母语,所以如果英语不好,我道歉 我是android开发的新手,我必须继续一个由前同事几个月前启动的android应用程序(他现在不在了) 在这个应用程序中,我们有一个用户列表和他们的地址,我必须通过点击这些地址来显示谷歌地图视图。即使谷歌地图(GoogleMap)吃午餐,也会引发一个例外 代码如下:Android Can';t在应用程序上显示谷歌地图视图,android,google-maps,Android,Google Maps,首先,你必须知道英语不是我的母语,所以如果英语不好,我道歉 我是android开发的新手,我必须继续一个由前同事几个月前启动的android应用程序(他现在不在了) 在这个应用程序中,我们有一个用户列表和他们的地址,我必须通过点击这些地址来显示谷歌地图视图。即使谷歌地图(GoogleMap)吃午餐,也会引发一个例外 代码如下: public class GMapsViewCopy extends MapActivity { private static final int DIALOG_ERR
public class GMapsViewCopy extends MapActivity
{
private static final int DIALOG_ERREUR = 100;
private MapView mapView;
private MapController mc;
private GeoPoint location;
/**
* Creation de l'activity
*/
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.gmapsview);
setTitle("Google Maps");
mapView = (MapView) this.findViewById(R.id.GMaps);
mapView.setBuiltInZoomControls(true);
String adresse = (String) this.getIntent().getStringExtra("adresse");
Log.i("", "Adresse : " + adresse);
location = this.getCoord(adresse);
if (location != null)
{
Log.i("", "location != null");
MonOverlay marker = new MonOverlay(getResources().getDrawable(R.drawable.marker));
marker.addPoint(location);
mapView.getOverlays().add(marker);
this.mc = this.mapView.getController();
this.mc.setCenter(this.location);
this.mc.setZoom(16);
} else
showDialog(DIALOG_ERREUR);
}
/**
* Permet de récuperer les coordonnées géographique du patient à partir de
* son adresse
*
* @param adresse
* @return GeoPoint
*/
private GeoPoint getCoord(String adresse)
{
GeoPoint point = null;
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try
{
List<Address> addresses = geoCoder.getFromLocationName(adresse, 5);
/*
* for(int i=0; i<addresses.size(); i++) { Log.i("",
* "Adresse["+i+"] : "+addresses.get(i)); }
*/
if (addresses.size() > 0)
{
point = new GeoPoint(
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
}
} catch (IOException e)
{
Log.i("", "exception : " + e);
e.printStackTrace();
}
return point;
}
public GeoPoint getLocation()
{
return location;
}
public void setLocation(GeoPoint location)
{
this.location = location;
this.mc.setCenter(this.location);
this.mapView.invalidate();
}
@Override
protected boolean isRouteDisplayed()
{
// TODO Auto-generated method stub
return false;
}
public class MonOverlay extends ItemizedOverlay<OverlayItem>
{
List<GeoPoint> points = new ArrayList<GeoPoint>();
public MonOverlay(Drawable defaultMarker)
{
super(boundCenterBottom(defaultMarker));
}
@Override
protected OverlayItem createItem(int i)
{
GeoPoint point = points.get(i);
return new OverlayItem(point, "titre", "description");
}
@Override
public int size()
{
// TODO Auto-generated method stub
return points.size();
}
public void addPoint(GeoPoint point)
{
this.points.add(point);
populate();
}
}
/**
* Permet de créer des boites de dialog
*/
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case DIALOG_ERREUR:
return new AlertDialog.Builder(GMapsViewCopy.this).setIcon(android.R.drawable.ic_menu_info_details)
.setTitle("Nous n'avons pas pu trouver l'adresse indiquée.")
.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
finish();
}
}).create();
}
return null;
}
/**
* Fermeture de l'activity
*/
protected void onDestroy()
{
super.onDestroy();
}
}
公共类GMapsViewCopy扩展了MapActivity
{
专用静态最终整数对话框\u ERREUR=100;
私有地图视图;
专用地图控制器;
私人地质点位置;
/**
*创作活动
*/
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u Picture);
setContentView(R.layout.gmapsview);
setTitle(“谷歌地图”);
mapView=(mapView)this.findViewById(R.id.GMaps);
mapView.SetBuilTinZoomControl(真);
字符串ADRESE=(字符串)this.getIntent().getStringExtra(“ADRESE”);
Log.i(“,”ADRESE:“+ADRESE”);
地点=此。getCoord(地址);
如果(位置!=null)
{
Log.i(“,”位置!=null”);
MonOverlay marker=newmonoverlay(getResources().getDrawable(R.drawable.marker));
标记。添加点(位置);
mapView.getOverlays().add(标记);
this.mc=this.mapView.getController();
this.mc.setCenter(this.location);
这个.mc.setZoom(16);
}否则
showDialog(DIALOG\u ERREUR);
}
/**
*患者与患者的合作关系
*儿子阿迪斯
*
*@param address
*@返回地球点
*/
专用地质点getCoord(字符串地址)
{
地球点=零;
Geocoder Geocoder=新的Geocoder(这个,Locale.getDefault());
尝试
{
列表地址=geoCoder.getFromLocationName(地址,5);
/*
*对于(int i=0;i 0)
{
点=新的地质点(
(int)(addresses.get(0.getLatitude()*1E6),
(int)(addresses.get(0.getLongitude()*1E6));
}
}捕获(IOE异常)
{
Log.i(“,”异常:“+e);
e、 printStackTrace();
}
返回点;
}
公共地质点位置()
{
返回位置;
}
公共空间设置位置(地质点位置)
{
这个位置=位置;
this.mc.setCenter(this.location);
this.mapView.invalidate();
}
@凌驾
受保护的布尔值isRouteDisplayed()
{
//TODO自动生成的方法存根
返回false;
}
公共类MonOverlay扩展了ItemizedOverlay
{
列表点=新的ArrayList();
公共单面显示器(可拉拔的默认标记)
{
super(boundCenterBottom(defaultMarker));
}
@凌驾
受保护的OverlayItem createItem(int i)
{
地质点=点。获取(i);
返回新的覆盖项(点,“滴度”、“说明”);
}
@凌驾
公共整数大小()
{
//TODO自动生成的方法存根
返回点。size();
}
公共无效添加点(地质点)
{
此.points.add(点);
填充();
}
}
/**
*对话的主题
*/
受保护的对话框onCreateDialog(int id)
{
开关(id)
{
案例对话框\u错误:
返回新的AlertDialog.Builder(GMapsViewCopy.this).setIcon(android.R.drawable.ic\u菜单\u信息\u详细信息)
.setTitle(“我们的雅芳之路”和印度之路)
.setPositiveButton(“确定”,新的DialogInterface.OnClickListener()
{
public void onClick(对话框接口对话框,int whichButton)
{
完成();
}
}).create();
}
返回null;
}
/**
*活动度
*/
受保护的空onDestroy()
{
super.ondestory();
}
}
我的舱单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cba.myandroid"
android:versionCode="1"
android:versionName="1.0">
<application
android:icon="@drawable/icon"
android:theme="@android:style/Theme.Light"
android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".GMapsView"
android:theme="@android:style/Theme.Light"
android:label="@string/app_name">
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
和my gmapview.xml(res/layout):
谢谢您的帮助:)首先,您的例外情况是什么? 其次,如果您在调试器上进行测试,请确保您使用的是基于GoogleAPI的AVD。基于标准平台的AVD无法运行谷歌地图视图。为此,您需要下载带有ADT接口的额外软件包。
第三,您需要在gmapview.xml中填写api密钥android:apiKey。您可以在此处找到有关这一点的更多信息:感谢您的回答。我说的是这个函数:protecteddialog onCreateDialog(intid)当我在一个地址上clic时,会出现这个错误对话框。关于我的API密钥,我自愿将“myKey”放在了iNtance的logcat窗口中(你知道我不想向所有人显示我的密钥^^),我是在请求你遇到的异常情况。您是在设备上测试还是在模拟器上测试?如果是模拟器,你在使用谷歌API吗?我再次在真实设备(Galaxy s&SII和HTC desire)上测试,你有什么例外?没关系,我解决了我的问题。谢谢你的回答,拉斐尔
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="myKey"
/>
<LinearLayout android:id="@+id/zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>