2015-10-20 58 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已经停止了' –