android googlemap不会在真实设备中显示

时间:2014-08-09 05:56:17

标签: android google-maps genymotion

我在我的genimotion中使用它并且它显示正常,但在我的真实设备中它只是在屏幕:(,我配置我的代码很长时间但它仍然没有显示。

API_Key是对的,几乎是正确的,我想是因为我在genymotion中正常破坏。

这是我的代码:

package fu.capstone.hemo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import fu.capstone.hemo.R;
import fu.capstone.hemo.activities.HelpPage;
import fu.capstone.hemo.common.CalculateUtills;
import fu.capstone.hemo.dao.ActivityDAO;
import fu.capstone.hemo.dao.MenuActivityDAO;
import fu.capstone.hemo.dao.UserProfileDAO;
import fu.capstone.hemo.vo.ActivitiesEntity;
import fu.capstone.hemo.vo.MenuActivityEntity;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class SeeGoogleMap extends FragmentActivity {
    GoogleMap map;
    LocationManager mLocationManager;
    Location mlocation = null;
    AlertDialog.Builder builder;
    LocationListener listen;
    long timeInMills;
    TextView txtTime, txtDistance, txtCaloBurn, txtCountTime, txtMeterDone;
    Calendar cal;
    Button btnStartTime, btnStopTime;
    int profileID;
    MyCount timer;
    List<Location> listLocation;
    float distanceSum = 0;
    MenuActivityEntity menuEntity;
    ProgressBar pro;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_see_google_map);
        listLocation = new ArrayList<Location>();
        txtTime = (TextView) findViewById(R.id.txtTime);
        btnStartTime = (Button) findViewById(R.id.btnStartTime);
        btnStopTime = (Button) findViewById(R.id.btnStopTime);
        txtCountTime = (TextView) findViewById(R.id.txtCountTime);
        txtMeterDone = (TextView) findViewById(R.id.txtMeterDone);
        pro = (ProgressBar) findViewById(R.id.proActiTime);
        btnStopTime.setClickable(false);
        btnStopTime.setAlpha(0);
        findViewById(R.id.imgBackPress).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        startActivity(new Intent(getApplicationContext(),
                                HelpPage.class));
                    }
                });
        SharedPreferences prefs = getSharedPreferences("profileIdShared",
                Context.MODE_PRIVATE);
        profileID = prefs.getInt("profileId", 0);

        map = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();
        map.setMyLocationEnabled(true);

        listen = new LocationListener() {

            @Override
            public void onStatusChanged(String provider, int status,
                    Bundle extras) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                listLocation.add(location);
                LatLng lastLocation = new LatLng(location.getLatitude(),
                        location.getLongitude());
                map.moveCamera(CameraUpdateFactory.newLatLngZoom(lastLocation,
                        15));
                String cityName = null;
                Geocoder gcd = new Geocoder(getBaseContext(),
                        Locale.getDefault());
                List<Address> addresses = null;
                try {
                    addresses = gcd.getFromLocation(location.getLatitude(),
                            location.getLongitude(), 1);
                    if (addresses.size() > 0) {
                        int a = addresses.get(0).getMaxAddressLineIndex();
                        for (int i = 0; i < a; i++) {
                            if (i == 0) {
                                cityName = addresses.get(0).getAddressLine(i);
                            } else {
                                cityName = cityName + ", "
                                        + addresses.get(0).getAddressLine(i);
                            }
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                map.addMarker(new MarkerOptions().title("Vị trí hiện tại")
                        .snippet(cityName).position(lastLocation));

                Polyline line = map.addPolyline(new PolylineOptions()
                        .add(new LatLng(listLocation.get(
                                listLocation.size() - 1).getLatitude(),
                                listLocation.get(listLocation.size() - 1)
                                        .getLongitude()),
                                new LatLng(location.getLatitude(), location
                                        .getLongitude())).width(5)
                        .color(Color.RED));
                float distance = listLocation.get(listLocation.size() - 1)
                        .distanceTo(location);

                distanceSum += distance;
                txtMeterDone.setText(distanceSum + "");
                Toast.makeText(getApplicationContext(),
                        "location changed!" + listLocation.toString(), 2000)
                        .show();
            }
        };
        mLocationManager = (LocationManager) getSystemService(getApplicationContext().LOCATION_SERVICE);
        Toast.makeText(getApplicationContext(), "GPS: " + mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
                +"Wifi: "+ mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
                , 3000).show();
        String provider = LocationManager.NETWORK_PROVIDER;
        mlocation = mLocationManager.getLastKnownLocation(provider);
        if (mlocation == null) {
             mLocationManager.requestLocationUpdates(
             LocationManager.GPS_PROVIDER, 5000, 10, listen);
        }
        else{
            LatLng lastLocation = new LatLng(mlocation.getLatitude(),
                    mlocation.getLongitude());
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(lastLocation,
                    15));
        }

        btnStartTime.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                btnStartTime.setClickable(false);
                btnStartTime.setAlpha((float) 0.0);
                btnStopTime.setClickable(true);
                btnStopTime.setAlpha(1);
                timer = new MyCount(1 * 60 * 60 * 1000 + 30 * 60 * 1000, 1000);
                mLocationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER, 5000, 10, listen);
                timer.start();
            }
        });
        btnStopTime.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                btnStartTime.setClickable(true);
                btnStartTime.setAlpha((float) 1.0);
                mLocationManager.removeUpdates(listen);
                try {
                    timer.cancel();
                } catch (Exception ex) {
                }
                btnStopTime.setAlpha(0);
                btnStopTime.setClickable(false);
            }
        });
    }

    @SuppressLint("DefaultLocale")
    class MyCount extends CountDownTimer {
        long timee;
        int count = 0;
        int ho = 0, mi = 0, se = 0;
        String ho1 = "", mi1 = "", se1 = "";

        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            timee = millisInFuture;
        }

        public void onFinish() {
            txtTime.setText("Hoàn thÀNH!");
            txtMeterDone.setText("00:00:00");
            this.cancel();
            // Use Intent to Navigate from this activity to another
        }

        @SuppressLint("DefaultLocale")
        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub
            long millis = millisUntilFinished;
            count++;
            if (count < 10) {
                se1 = "0" + count;
            } else {
                se1 = "" + count;
            }
            if (count > 60) {
                mi += 1;

            }
            if (mi < 10) {
                mi1 = "0" + mi;
            } else {
                mi1 = "" + mi;
            }
            if (mi > 60) {
                ho += 1;
            }
            ho1 = "0" + ho;
            txtCountTime.setText(ho1 + ":" + mi1 + ":" + se1);
            mLocationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 5000, 10, listen);
            String hms = String.format(
                    "%02d:%02d:%02d",
                    TimeUnit.MILLISECONDS.toHours(millis),
                    TimeUnit.MILLISECONDS.toMinutes(millis)
                            - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
                                    .toHours(millis)),
                    TimeUnit.MILLISECONDS.toSeconds(millis)
                            - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
                                    .toMinutes(millis)));
            txtTime.setText(hms);
            pro.setProgress(count);

        }

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        mLocationManager.removeUpdates(listen);
        super.onPause();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        mLocationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 5000, 10, listen);
        super.onResume();
    }
}

这是我的表现:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="fu.capstone.hemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
<activity
            android:name="fu.capstone.hemo.SeeGoogleMap"
            android:label="@string/title_activity_see_google_map" >
        </activity>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyDdkM-X-ZCQm8qq3CddIViFzTzHVaeOZtQ" />
    </application>

解决:感谢## Arun Antoney ##,只导出签名的APK然后在显示的最后一步注册SHA-1,它运行得像一个chamed:3

2 个答案:

答案 0 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

https://developers.google.com/maps/documentation/android/start

1.我认为问题在于获取API密钥或使用SHA-1指纹。

2.您是否在Google Consol

中为应用程序包提供了SHA-1指纹

3.将Google Play服务安装到您的模拟器或Android设备中。

4.请使用Android设备进行地图测试或Genymotion模拟器。

答案 1 :(得分:-1)

使用项目密钥库文件和软件包名称创建新的api密钥。然后尝试,因为使用 debug.keystore 创建的api将仅在调试模式下显示地图