2015-10-20 44 views
2

標記我下面toturial在Google Developers網站給出在MapsActivity.java谷歌地圖不顯示在android系統

package com.pro.soft.inzi.track_trace; 

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

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 

     Button sign_in_btn = (Button) findViewById(R.id.b1); 
     sign_in_btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(getApplicationContext(),Live.class); 
       startActivity(intent); 
      } 
     }); 
     Button history_btn = (Button) findViewById(R.id.b2); 
     history_btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(getApplicationContext(), History.class); 
       startActivity(intent); 
      } 
     }); 
    } 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 

     // Add a marker in Pakistan and move the camera 
     LatLng Pakistan = new LatLng(31, 71); 
     googleMap.addMarker(new MarkerOptions().position(Pakistan).title("Marker in Pakistan")); 
     googleMap.moveCamera(CameraUpdateFactory.newLatLng(Pakistan)); 
    } 
} 

這些都是AndroidManifest.xml文件

<uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
    <!-- 
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use 
     Google Maps Android API v2, but are recommended. 
    --> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

    <!-- 
    To retrieve OAuth 2.0 tokens or invalidate tokens to disconnect a user. This disconnect 
    option is required to comply with the Google+ Sign-In developer policies 
    --> 
    <uses-permission android:name="android.permission.USE_CREDENTIALS" /> 
    <!-- To retrieve the account name (email) as part of sign-in: --> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <!-- To auto-complete the email text field in the login form with the user's emails --> 
    <uses-permission android:name="android.permission.READ_PROFILE" /> 
    <uses-permission android:name="android.permission.READ_CONTACTS" /> 

    <permission 
     android:name="your.package.name.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature" /> 
    <uses-permission android:name="your.package.name.permission.MAPS_RECEIVE"/> 
    <uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true"/> 

的一切權限,該代碼除了一個Marker做工精細我遵循這個網站上的相關問題和網絡上的說明但是都是徒勞的。
標記未顯示在地圖中 請告訴我什麼是錯的。

編輯:這裏是activity_maps代碼,包括MapFragment在它

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

    <Button 
     android:id="@+id/b1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/b1_text" 
     /> 
    <Button 
     android:id="@+id/b2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/b2_text" 
     android:layout_toRightOf="@+id/b1"/> 
    <Button android:id="@+id/b3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/b3_text" 
     android:layout_toRightOf="@+id/b2"/> 

    <RelativeLayout 
     android:id="@+id/mainP" 
     android:layout_width="match_parent" 
     android:layout_height="400dp" 
     android:layout_below="@+id/b2"> 

     <fragment 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:id="@+id/map" 
      tools:context=".MapsActivity" 
      android:name="com.google.android.gms.maps.MapFragment" /> 

    </RelativeLayout> 
    <EditText 
     android:id="@+id/info" 
     android:layout_width="160dp" 
     android:layout_height="wrap_content" 
     android:hint="@string/info_1" 
     android:layout_below="@+id/mainP" 
     /> 
    <EditText 
     android:layout_width="160dp" 
     android:layout_height="wrap_content" 
     android:hint="@string/info_2" 
     android:layout_below="@+id/mainP" 
     android:layout_toRightOf="@+id/info"/> 
</RelativeLayout> 

回答

4

首先,你應該初始化您googleMap ObjectonCreate(....)

SupportMapFragment googleMap=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); 
+0

碼我現在不過還是加入這個'Marker'不可見 –

+0

在上面的代碼中,你發佈了SupportMapFragment的'googleMap',因爲當我將鼠標懸停在它上面時,tooltiptext顯示'變量googleMap永遠不會被使用' –

+1

@InzimamTariqIT使'googleMap對象全局' –

1

試試這個

googleMap.addMarker(new MarkerOptions() 
      .position(Pakistan) 
      .title("Marker in Pakistan") 
      .icon(BitmapDescriptorFactory 
      .defaultMarker(BitmapDescriptorFactory.HUE_GREEN))); 
+0

這也沒有工作 –

+1

在哪裏找到你的谷歌地圖 –

0
add this ,it works 

googleMap.addMarker(新的MarkerOptions().POSITION(新經緯度(緯度,經度))。title(「我的位置」).snippet(「kochi」)。draggable(true));

1

onMapReady(GoogleMap googleMap)不會被調用。 你需要調用getMapAsync在地圖上的片段設置OnMapReadyCallback

the documentation

公共無效getMapAsync(OnMapReadyCallback回調)

設置將被觸發時的GoogleMap的回調對象 實例已準備就緒可以使用。

onCreate函數的末尾添加以下代碼:

MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map); 
mapFragment.getMapAsync(this); 
+0

當我在onCreate()中添加此項時,該活動顯示一個包含「不幸的MapsActivity已停止」的對話框,爲什麼是 –

+1

@ Inzimam Tariq IT很難說沒有看到錯誤,問題是什麼,你能發佈你的activity_maps.xml和錯誤的堆棧跟蹤嗎? – antonio

+0

我加了我的'activity_maps'代碼。它不會產生錯誤/堆棧跟蹤但是當活動開始時彈出窗口出現'不幸的是,MapsActivity已經停止了' –