2013-05-30 32 views
1

enter image description here如何在Android的MapView類

顯示地圖我創建的Android mapview.but顯示灰度的mapview.iam使用谷歌的MapView自己的API密鑰的谷歌地圖。

如何在沒有灰度的mapview中獲取地圖?

public class MainActivity extends MapActivity { 

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

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

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


} 


<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" > 

<com.google.android.maps.MapView 
    android:id="@+id/map" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:apiKey="AIzaSyBNpjyktHItZYm4no0wTvzSv8tWU5gaoy0" /> 

</RelativeLayout> 

這是我的代碼,它的顯示灰度。如何解決這個問題?

http://wptrafficanalyzer.in/blog/showing-current-location-using-onmylocationchangelistener-in-google-map-android-api-v2/我指的這個鏈接,但enter image description here

+5

請注意,您正在嘗試使用已棄用的Maps V1。請考慮使用Maps V2。除此之外,您可能會考慮發佈屏幕截圖,顯示您所看到的效果。 – CommonsWare

+0

Divya您可能會在顯示屏上顯示拼貼,因爲當我使用此代碼時,我得到的結果相同,我嘗試了2天。 – Lucifer

+0

你有什麼想法嗎? – android

回答

2

首先,CommonsWare說,你應該使用V2地圖API顯示空白輸出。你什麼時候要求鑰匙?您不能在2013年3月之後申請Maps v1密鑰。我懷疑您可能會嘗試在v1 API中使用v2密鑰。檢查你的logcat,看看是否有消息。

Google Maps API (v2)

另一種可能性是,你註冊的鍵不正確。你確定你有右鍵的SHA1指紋嗎?如果您在可用時註冊了v1密鑰,您確定使用您註冊的同一個密鑰簽名了嗎?

另外 - 它看起來像,根據你的狀態欄,你沒有得到很好的連接。 Google地圖需要網絡連接。嘗試打開wifi來檢查它是否工作。如果是這樣,很可能你只是沒有得到下載的地圖。如果wifi工作,嘗試讓應用程序坐下來關閉wifi,看看地圖是否最終加載3G。

+0

我用谷歌地圖2,但其顯示空白mapview。 – android

2
check below example it working... 

[example link1][1] 

[example link2][2] 

[example link3][3] 

    - manifest : Provide all need Permision in This Example i applied Find And Navigation 



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

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

    <uses-permission android:name="com.example.demomap1.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-feature 
     android:glEsVersion="0x00020000" 
     android:required="true" /> 


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

    <uses-permission android:name="com.example.mapv2demo1.permission.MAPS_RECEIVE" /> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.CAMERA" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.READ_CALENDAR" /> 
    <uses-permission android:name="android.permission.WRITE_CALENDAR" /> 

    <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" > 
     <uses-library android:name="com.google.android.maps" /> 
     <activity 
      android:name="com.example.mapv2demo1.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=" Put Key Here" /> 
    </application> 

</manifest> 

    - Layout xml---I use the com.google.android.gms.maps.SupportMapFragment to show map 

<LinearLayout 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" 
    android:orientation="vertical" 
    tools:context=".MainActivity" > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/btn_find" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBottom="@+id/et_location" 
      android:layout_alignParentRight="true" 
      android:text="find" /> 

     <EditText 
      android:id="@+id/et_location" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:inputType="text" 
      android:hint="Enter Location" 
      android:layout_toLeftOf="@id/btn_find" /> 

    </RelativeLayout> 

    <fragment 
     android:id="@+id/mapview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     class="com.google.android.gms.maps.SupportMapFragment" /> 

</LinearLayout> 


    - Code to integration Map v2 in code the Intent is call the map app which install in phone.. 

    import android.content.Intent; 
    import android.net.Uri; 
    import android.os.Bundle; 
    import android.support.v4.app.FragmentActivity; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 
    import android.widget.EditText; 

    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; 

    public class MainActivity extends FragmentActivity implements OnClickListener { 
     GoogleMap mapView; 
     LatLng latLong; 
     MarkerOptions markerOption; 
     Button btnFind; 
     EditText etLocation; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      mapView = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
        R.id.mapview)).getMap(); 

      btnFind=(Button)findViewById(R.id.btn_find); 
      etLocation=(EditText)findViewById(R.id.et_location); 
      btnFind.setOnClickListener(this); 

     } 
     @Override 
     public void onClick(View v) { 
      String destination=etLocation.getText().toString(); 
      Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
        Uri.parse("http://maps.google.com/maps?daddr=" 
          + destination)); 
      startActivity(intent); 

     } 
    } 
    [1]: http://karnshah8890.blogspot.in/2013/02/google-maps-android-api-v2.html 
    [2]: http://android-er.blogspot.in/2012/12/a-simple-example-using-google-maps.html 
    [3]: http://wptrafficanalyzer.in/blog/showing-current-location-in-google-maps-using-api-v2-with-supportmapfragment/ 
+0

我也試過第三個鏈接,但它顯示空白view.how來解決這個問題? – android

+0

@Divya第一次檢查你必須在地圖V2 Api從谷歌控制檯Api和第二你檢查你的密鑰,並嘗試第一鏈接,我把我測試它的代碼工作...注意: - 將API_KEY在Menifest <元數據 android:name =「com.google.android.maps.v2.API_KEY」 android:value =「Put Key Here」/> – Narendra

+0

是它的工作,但我需要在mapview片段中顯示地圖 – android