Android、谷歌地图不显示
我正在制作一个应用程序,需要显示我用纬度和经度定义的位置。一切顺利,它在我的应用程序屏幕中打开谷歌地图,但地图不会显示! 这是我使用的代码...
public class MapsActivity extends MapActivity {
MapView mapView;
MapController mc;
GeoPoint p;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//vraag custom title bar
requestCustomTitle();
setContentView(R.layout.mapsactivity);
//Titel dynamisch invullen volgens taalvoorkeuren, roept methode setCustomTitle op
setCustomTitle(getResources().getString(R.string.MapsTitel));
showZoom();
locate(getIntent().getStringExtra("kantoorLat"), getIntent().getStringExtra("kantoorLng"));
addMarker();
}
/*
* De methode showZoom zorgt ervoor dat de zoombuttons worden getoond
*/
private void showZoom(){
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.displayZoomControls(true);
}
/*
* De methode locate zorgt ervoor dat de juiste locatie wordt getoond
*/
private void locate(String strlat, String strlng){
mc = mapView.getController();
String coordinates[] = {strlat,strlng};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
mapView.invalidate();
}
/*
* De methode addMarker zorgt ervoor dat er een indicator staat op de locatie
*/
private void addMarker(){
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
}
protected boolean isRouteDisplayed(){
return false;
}
class MapOverlay extends com.google.android.maps.Overlay{
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when){
super.draw(canvas, mapView, shadow);
Point screenPts = new Point();
mapView.getProjection().toPixels(p,screenPts);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.accentindicator);
canvas.drawBitmap(bmp, screenPts.x+7, screenPts.y-19, null);
return true;
}
}
I'm making an app that needs to show a location that I define with latitude and longitude. Everything goes fine, it opens Google maps in my app screen but the maps won't show up!
This is the code I use...
public class MapsActivity extends MapActivity {
MapView mapView;
MapController mc;
GeoPoint p;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//vraag custom title bar
requestCustomTitle();
setContentView(R.layout.mapsactivity);
//Titel dynamisch invullen volgens taalvoorkeuren, roept methode setCustomTitle op
setCustomTitle(getResources().getString(R.string.MapsTitel));
showZoom();
locate(getIntent().getStringExtra("kantoorLat"), getIntent().getStringExtra("kantoorLng"));
addMarker();
}
/*
* De methode showZoom zorgt ervoor dat de zoombuttons worden getoond
*/
private void showZoom(){
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.displayZoomControls(true);
}
/*
* De methode locate zorgt ervoor dat de juiste locatie wordt getoond
*/
private void locate(String strlat, String strlng){
mc = mapView.getController();
String coordinates[] = {strlat,strlng};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
mapView.invalidate();
}
/*
* De methode addMarker zorgt ervoor dat er een indicator staat op de locatie
*/
private void addMarker(){
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
}
protected boolean isRouteDisplayed(){
return false;
}
class MapOverlay extends com.google.android.maps.Overlay{
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when){
super.draw(canvas, mapView, shadow);
Point screenPts = new Point();
mapView.getProjection().toPixels(p,screenPts);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.accentindicator);
canvas.drawBitmap(bmp, screenPts.x+7, screenPts.y-19, null);
return true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
MD5 密钥不能在不同的机器上使用。
您需要为当前使用的 SDK 生成 MD5 调试密钥。
为您当前的机器生成调试密钥并重试。
MD5 key will not work on different machines.
You need to generate MD5 debug key for the current SDK that you are using.
Generate the debug key for your current machine and try again.
获取API 密钥
在http://code.google.com /apis/maps/signup.html
将以下规则放入您的 AndroidManifest 中
Get a API key here
http://code.google.com/apis/maps/signup.html
Put the following rule in your AndroidManifest
检查以下博客条目,其中列出了可能导致此问题的原因。
http://blog.doityourselfandroid。 com/2011/01/18/using-google-maps-in-your-android-app/
除了 Activity 之外,清单、布局或 API 密钥也可能无效。
简而言之
支持谷歌地图。
组件需要添加到您的
布局,包含有效的 Google
地图 API 密钥
负责展示地图
需要从 MapActivity
应用程序清单需要
设置与
android.permission.INTERNET
权限
需要设置
com.google.android.maps 库
在您的情况下,很可能是互联网连接(清单和/或模拟器互联网访问中的许可)或无效的 API 密钥。请记住,API 密钥链接到用于签署 APK 的证书。
Check the following blog entry that lists down the reasons that might cause this issue.
http://blog.doityourselfandroid.com/2011/01/18/using-google-maps-in-your-android-app/
Besides the activity, its also the manifest, the layout or the API key that can be invalid.
In short
support Google Maps.
component needs to be added to your
layout, containing a valid Google
Maps API key
be responsible for showing the map
needs to extend from MapActivity
application manifest needs to be
setup with the
android.permission.INTERNET
permission
needs to be setup with the
com.google.android.maps library
In your case, it's most likely internet connectivity (permisson in manifest and/or emulator internet access) or an invalid API key. Keep in mind that the API key is linked to the certificate used to sign your APK.
您需要获得密钥才能在应用程序中访问 Google 地图,获得密钥后,您必须在 ue 清单文件中添加库,当您获得密钥时,该库将自动安装。
You need to get the key to access Google map in your application and after getting key you have to add library in ue manifest file which will be installed automatic when u got the key.
我认为它是您的模拟器版本问题。Android sdk 2.2 模拟器有时不显示确切位置。尝试运行模拟器 2.1 。
I think its your emulator version problem.Android sdk 2.2 emulators some times doesnot show the exact location.Try to run emulator 2.1 .
即使使用Google API简介我也遇到过这样的问题。
我遵循本教程,它对我有用:
http:// www.vogella.com/articles/AndroidGoogleMaps/article.html
祝你好运,
Even using the Google API Introduction I have had problems like this.
I followed this tutorial and it worked for me:
http://www.vogella.com/articles/AndroidGoogleMaps/article.html
Good luck,