Android mapView在手机和模拟器中不同 [英] Android mapView different in phone and emulator

查看:20
本文介绍了Android mapView在手机和模拟器中不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于应用程序:--这是一个简单的应用程序,可以找到用户当前的位置.

About App:--This is a simple app which find the user current location.

问题:-- 该应用在模拟器上运行良好,请看图片.

Problem:-- The app works fine on emulator please see the image.

但在手机中它没有显示 MapView.请看图片.

But in phone it is not showing the MapView.Please see the image.

请告诉我手机出了什么问题.在 Phone 中,它只是下载大量 (20 mb) 数据,但不显示实际地图.

Please tell me what is going wrong with the phone. In Phone it just download the huge(20 mb) data but not show the actual map.

我正在获取 Logcat -

Logcat im getting -

   10-31 16:44:45.994: E/MapActivity(3026): Couldn't get connection factory client

  10-31 15:47:42.319: ERROR/MapView(1773): java.lang.IllegalStateException: Null Bitmap!   "loading_tile"; if seen during a test, this usually means that the image file needs to be added to the test.config file

XML 文件

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<TextView  
android:id="@+id/myLocationText"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" />

 <com.google.android.maps.MapView
 android:id="@+id/myMapView"
 android:layout_height="fill_parent"
 android:layout_width="fill_parent"
 android:clickable="true"
 android:enabled="true"
 android:apiKey="0bBgLl42nWwnTf983Y5VdIgfZI6mC7meL7Ms_qg"/>

</LinearLayout>

代码

public class WhereIam extends MapActivity {
MapController mapController;
MyPositionOverlay positionOverlay;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    MapView myMapView=(MapView)findViewById(R.id.myMapView);
    mapController=myMapView.getController();
    myMapView.setSatellite(true);
    myMapView.setStreetView(true);
    myMapView.displayZoomControls(false);
    myMapView.setBuiltInZoomControls(true);
    mapController.setZoom(16);
    positionOverlay = new MyPositionOverlay();
    List<Overlay> overlays = myMapView.getOverlays();
    overlays.add(positionOverlay);
    LocationManager locationManager;
    String context=Context.LOCATION_SERVICE;
    locationManager=(LocationManager)getSystemService(context);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    String provider = locationManager.getBestProvider(criteria, true);
    Location location = locationManager.getLastKnownLocation(provider);

    final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
        updateWithNewLocation(location);
        }
        public void onProviderDisabled(String provider){
        updateWithNewLocation(null);
        }
        public void onProviderEnabled(String provider){ }
        public void onStatusChanged(String provider, int status,
        Bundle extras){ }
        };

    updateWithNewLocation(location);
    locationManager.requestLocationUpdates(provider, 2000, 10,
            locationListener);
}

private void updateWithNewLocation(Location location) {
    String latLongString;
    TextView myLocationText;
    String addressString ="No Address Found";
    myLocationText=(TextView)findViewById(R.id.myLocationText);
    if(location!=null)  {
        // Update the map location.
        positionOverlay.setLocation(location);
        Double geoLat = location.getLatitude()*1E6;
        Double geoLng = location.getLongitude()*1E6;
        GeoPoint point = new GeoPoint(geoLat.intValue(),
        geoLng.intValue());
        mapController.animateTo(point);
        double lat=location.getLatitude();
        double lng=location.getLongitude();
        latLongString = "Lat:" + lat + "
Long:" + lng;
        Geocoder gc=new Geocoder(this, Locale.getDefault());
        try {
            List<Address> addressess= gc.getFromLocation(lat, lng, 1);
            StringBuilder sb=new StringBuilder();
            if(addressess.size()>0) {
                Address address=addressess.get(0);
                for(int i=0;i<address.getMaxAddressLineIndex();i++) {
                    sb.append(address.getAddressLine(i)).append("
");
                    sb.append(address.getLocality()).append("
");
                    sb.append(address.getPostalCode()).append("
");
                    sb.append(address.getCountryName());
                }
                addressString = sb.toString();
            }
        }catch(IOException e) {} 
    }
    else {
        latLongString="No Found Location";
    }
    myLocationText.setText("Your current Location is 
"+latLongString+"
"+addressString);
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

}

我只是按照 goto->cmd 提示创建了一个 api 密钥

I just crated one api key as follow goto->cmd prompt

将目录更改为keytool文件夹

change the directory to the keytool folder

现在运行命令 keytool -list -alias androiddebugkey -keystore "C:Userspc.androiddebug.keystore" -storepass android -keypass android

now run the command keytool -list -alias androiddebugkey -keystore "C:Userspc.androiddebug.keystore" -storepass android -keypass android

查看图片

现在我转到注册页面,我只需将 MD5 放入 EditText 并检查接受并单击生成密钥,然后出现以下页面,显示密钥--

Now i goto signup page i simply put the MD5 in EditText and checked accept and clicked generate key then following page comes up which show the key--

现在我把这个键放在我的 MapView Xml 文件中..

Now i put this key in my MapView Xml file..

您可以看到应用程序在模拟器中运行良好,但在真实手机中却没有.

as you can see app running fine in emulator but not in real phone.

我哪里出错了,如何为真手机生成第二个 API 密钥?

Where am I going wrong, and how do I generate a second API key for a real phone?

推荐答案

要在真实硬件上进行测试,您需要使用与调试密钥不同的密钥.在您的帖子之后,您正在使用调试密钥库.

For testing on real hardware you need to use a key different from the debug key. Following your post you are using a debug keystore.

https://developers.google.com/maps/documentation/android-api/v1/?csw=1#getfingerprint

在谷歌上阅读该段落.

应用程序导出为具有用于 google api 的相同密钥(而不是调试密钥)的签名应用程序很重要.

Is important that the application is exported as a signed application with the same key used for the google api (and NOT the debug key).

对于创建一个有效的阅读该段落:

For create a valid read that paragraph:

http://developer.android.com/guide/publishing/app-signing.html#releasemode

然后使用同一个key获取google maps api key.

and then use the same key to obtain the google maps api key.

获得应用程序的密钥后,您可以通过在项目上单击鼠标右键选择 Android 工具,然后导出为已签名的应用程序包并按照向导来导出已签名的应用程序(可能您可以创建一个新的键使用该向导).

Once you have the key for your application you can export a signed application by clicking with right button of your mouse on the project select Android Tools and then Export as a signed application package and follow the wizard (probably you can create a new key using that wizard).

希望有所帮助.

这篇关于Android mapView在手机和模拟器中不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆