2013-05-25 345 views
0

我一直在關注這個教程Showing current location in Google Maps using API v2但無論我做什麼,我的地圖只是沒有顯示。除縮放按鈕外,屏幕全是灰色的。我已經添加了互聯網權限,我已經生成了API密鑰(v2)十幾次(我選擇「創建新的Android密鑰...」並輸入SHA1證書指紋,它會生成好的),我的GPS已啓用。 ..我不知道該做什麼了。這裏是我的代碼&清單:android google地圖顯示當前位置;地圖不顯示

package com.example.myexample; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
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 android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.app.Dialog; 
import android.support.v4.app.FragmentActivity; 
import android.widget.TextView; 

public class MainActivity extends FragmentActivity implements LocationListener { 

    GoogleMap googleMap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

    // Getting Google Play availability status 
     int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext()); 

     if(status!=ConnectionResult.SUCCESS){ 
      int requestCode = 10; 
      Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode); 
      dialog.show(); 
     } 

     else { 

      // Getting reference to the SupportMapFragment of activity_main.xml 
      SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
      // Getting GoogleMap object from the fragment 
      googleMap = fm.getMap(); 
      // Enabling MyLocation Layer of Google Map 
      googleMap.setMyLocationEnabled(true); 
      // Getting LocationManager object from System Service LOCATION_SERVICE 
      LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
      // Creating a criteria object to retrieve provider 
      Criteria criteria = new Criteria(); 
      // Getting the name of the best provider 
      String provider = locationManager.getBestProvider(criteria, true); 
      // Getting Current Location 
      Location location = locationManager.getLastKnownLocation(provider); 

      if(location!=null){ 
       onLocationChanged(location); 
      } 
      locationManager.requestLocationUpdates(provider, 20000, 0, this); 
     } 
    } 

    @Override 
    public void onLocationChanged(Location location) { 

     TextView tvLocation = (TextView) findViewById(R.id.tv_location); 
     // Getting latitude of the current location 
     double latitude = location.getLatitude(); 
     // Getting longitude of the current location 
     double longitude = location.getLongitude(); 
     // Creating a LatLng object for the current location 
     LatLng latLng = new LatLng(latitude, longitude); 
     // Showing the current location in Google Map 
     googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
     // Zoom in the Google Map 
     googleMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 
     // Setting latitude and longitude in the TextView tv_location 
     tvLocation.setText("Latitude:" + latitude + ", Longitude:"+ longitude); 
    } 

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

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

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

清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.myexample" 
    android:versionCode="1" 
    android:versionName="1.0" > 

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

    <permission 
     android:name="com.example.myexample.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature"/> 

    <uses-permission android:name="com.example.myexample.permission.MAPS_RECEIVE"/> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 

    <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="com.example.myexample.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="my api"/> 

    </application> 

</manifest> 
+0

你會得到例外或錯誤嗎? – Raghunandan

+0

看到http://stackoverflow.com/questions/16752015/how-would-i-make-my-google-maps-app-start-with-zoom-on-my-current-location/16752081#16752081 –

+0

有setMyLocationEnabled(true)的錯誤; - 它總是返回零 –

回答

0

試試這個代碼,希望這將有助於

MainActivity.java

public class MyActivity extends FragmentActivity implements LocationListener { 

    GoogleMap googleMap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //show error dialog if GoolglePlayServices not available 
     if (!isGooglePlayServicesAvailable()) { 
      finish(); 
     } 
     setContentView(R.layout.activity_my); 
     SupportMapFragment supportMapFragment = 
       (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap); 
     googleMap = supportMapFragment.getMap(); 
     googleMap.setMyLocationEnabled(true); 
     LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 
     String bestProvider = locationManager.getBestProvider(criteria, true); 
     Location location = locationManager.getLastKnownLocation(bestProvider); 
     if (location != null) { 
      onLocationChanged(location); 
     } 
     locationManager.requestLocationUpdates(bestProvider, 20000, 0, this); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     TextView locationTv = (TextView) findViewById(R.id.latlongLocation); 
     double latitude = location.getLatitude(); 
     double longitude = location.getLongitude(); 
     LatLng latLng = new LatLng(latitude, longitude); 
     googleMap.addMarker(new MarkerOptions().position(latLng)); 
     googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
     googleMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 
     locationTv.setText("Latitude:" + latitude + ", Longitude:" + longitude); 
    } 

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

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

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

    private boolean isGooglePlayServicesAvailable() { 
     int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
     if (ConnectionResult.SUCCESS == status) { 
      return true; 
     } else { 
      GooglePlayServicesUtil.getErrorDialog(status, this, 0).show(); 
      return false; 
     } 
    } 
} 

其XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 

    <fragment 
     android:id="@+id/googleMap" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     class="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_above="@+id/latlongLocation" /> 

    <TextView 
     android:id="@+id/latlongLocation" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="bottom" 
     android:layout_alignParentBottom="true" 
     android:background="#ff058fff" 
     android:paddingTop="5dp" 
     android:paddingBottom="5dp" 
     android:textColor="#ffffffff" 
     android:paddingLeft="5dp" 
     android:paddingRight="5dp" /> 
</RelativeLayout>