2015-02-08 88 views
0

這是代碼。我在屏幕上方的textview中獲取經度和緯度。出現googel符號。但地圖不會出現,而是出現白色屏幕。我覺得在這個代碼網絡提供商是用來代替互聯網提供商。這是很好的得到一張地圖,永遠。爲什麼我會使用白色屏幕而不是地圖?

MApActivity

import android.app.Dialog; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.view.Menu; 
import android.widget.TextView; 
import android.widget.Toast; 
    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; 

public class MapActivity extends FragmentActivity implements LocationListener { 

GoogleMap googleMap; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_map); 
    // Getting Google Play availability status 
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext()); 

    // Showing status 
    if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available 

     int requestCode = 10; 
     Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode); 
     dialog.show(); 
     Toast.makeText(getBaseContext(),"No service", Toast.LENGTH_SHORT).show(); 

    }else { // Google Play Services are available 

     // 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(10)); 

    // 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 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.map, menu); 
    return true; 
} 
} 

的Manifest.xml 我addded所有要求正確,我猜。

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

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="21" /> 
<permission 
android:name="com.example.xxxx.permission.MAPS_RECEIVE" 
android:protectionLevel="signature" /> 

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



<android:uses-permission 
    android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
    android:maxSdkVersion="18" /> 
<android:uses-permission 
    android:name="android.permission.READ_EXTERNAL_STORAGE" 
    android:maxSdkVersion="18" /> 
<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=".MapActivity" 
     android:label="@string/title_activity_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="AIzaSyBv0UVaXXYiFGWBdNb_DNSQIdkI9672IlY" /> 
      <uses-library android:name="com.google.android.maps" /> 
</application> 

</manifest> 
+0

沒有設置在明顯的地圖API密鑰? 你打開了你的網絡連接嗎? – 2015-02-08 09:57:59

+0

是的。我添加了地圖鍵。我的WIFI已打開。 – User15 2015-02-08 10:09:31

+0

您是否設置了清單中的所有權限 – 2015-02-08 10:14:27

回答

0
Please check these permission in manifest 




<permission 
     android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature" /> 
    <uses-permission android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <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.WRITE_EXTERNAL_STORAGE" /> 

    <!-- Required to show current location --> 
    <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" /> 


      <meta-data 
    android:name="com.google.android.maps.v2.API_KEY" 
    android:value="your api key of gooogle console" />  
+0

我有這些在我的清單 – User15 2015-02-08 10:23:52

相關問題