2013-12-17 76 views
1

好吧,我一整天都在這裏,我放棄了試圖自己做這個!在地圖片段中顯示自定義文本,Android

我已經聲明瞭地圖和地點

private GoogleMap googleMap; 

宣佈

static final LatLng SECC = new LatLng(55.8607,-4.2871); 

生成的地圖

googleMap=((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 

內置標記位置

Marker secc =googleMap.addMarker(new MarkerOptions().position(SECC) 
      .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)) 
      .title("SECC").snippet("Exhibition Way, Glasgow, G3 8YW\nSports: Boxing, Gymnastics, Judo, Netball, Wrestling, Weightlifting")); 

設置上點擊收聽到地圖

googleMap.setOnMarkerClickListener((OnMarkerClickListener)this); 

設置自定義信息框

googleMap.setInfoWindowAdapter(new InfoWindowAdapter() 
     { 
     @Override 
     public View getInfoWindow(Marker arg0) 
      { 
      return null; 
      } 

     public View getInfoContents(Marker marker) 
      { 
      View v = getLayoutInflater().inflate(R.layout.marker,null); 

      TextView info=(TextView) v.findViewById(R.id.info); 

      info.setText("hello"); 
      return v; 
      } 
     }); 

,併成立了點擊事件

googleMap.setOnMarkerClickListener(new OnMarkerClickListener() 
     { 
      @Override 
      public boolean onMarkerClick(Marker marker) 
      { 
       marker.showInfoWindow(); 
       return true; 
      } 
    }); 

任何人都可以指出我在哪裏會出錯嗎?地圖載入,您可以點擊一個標記,以獲得一個片段,但自定義信息顯示不

編輯包括清單

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.muc_coursework" 
android:versionCode="1" 
android:versionName="1.0" >  
<uses-sdk 
    android:minSdkVersion="19" 
    android:targetSdkVersion="19" /> 
<permission 
    android:name="org.me.myandroidstuff.mapstuff.permission.MAPS_RECEIVE" 
    android:protectionLevel="signature"/> 
<uses-feature 
    android:glEsVersion="0x00020000" 
    android:required="true"/> 
<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"/> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.muc_coursework.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="AIzaSyBFL6EfXODsfCxqyNktI5k9m03rygILej4"/> 
    <meta-data 
android:name="com.google.android.gms.version" 
android:value="@integer/google_play_services_version" /> 
</application> 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<fragment 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:name="com.google.android.gms.maps.MapFragment"/> 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 
<TextView 
    android:id="@+id/info" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center"/> 

回答

5

以下代碼可能對您有所幫助。

公共類Stacky延伸FragmentActivity實現OnMarkerClickListener {

靜態最終經緯度SECC =新經緯度(55.8607,-4.2871); 私有GoogleMap mMap;

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


    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map2)).getMap(); 
    mMap.setMyLocationEnabled(true); 

    setUpMap(); 
     findSMSLocation(); 
    // Setting a custom info window adapter for the google map  
    mMap.setInfoWindowAdapter(new InfoWindowAdapter() { 

     // Use default InfoWindow frame 
     @Override 
     public View getInfoWindow(Marker marker) {     
      return null; 
     }   

     // Defines the contents of the InfoWindow 
     @Override 
     public View getInfoContents(Marker marker) { 

      // Getting view from the layout file info_window_layout 
      View v = getLayoutInflater().inflate(R.layout.custom_info, null); 

       TextView tvLat = (TextView) v.findViewById(R.id.info);  
       tvLat.setText("info"); 
       tvLat.setTextColor(Color.GREEN); 

      return v; 
     } 

    }); 


} 

@Override 
protected void onPause() { 
    super.onPause(); 

} 

@Override 
protected void onResume() { 
    super.onResume(); 
    setUpMapIfNeeded(); 
} 


public void findSMSLocation() { 
    // TODO Auto-generated method stub 
    try{ 

      mMap.addMarker(new MarkerOptions().position(SECC).title("SECC").snippet("Exhibition Way, Glasgow, G3 8YW\nSports: Boxing, Gymnastics, Judo, Netball, Wrestling, Weightlifting")); 

      mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(SECC, 18.0f)); 

    }catch(Exception e){ 

     e.printStackTrace(); 
    } 

} 

private void setUpMapIfNeeded() { 
    // Do a null check to confirm that we have not already instantiated the map. 
    if (mMap == null) { 
     // Try to obtain the map from the SupportMapFragment. 
     mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map2)) 
       .getMap(); 
     mMap.setMyLocationEnabled(true); 
     // Check if we were successful in obtaining the map. 
     if (mMap != null) { 
      setUpMap(); 
     } 
    } 
} 



private void setUpMap() { 
    mMap.getUiSettings().setCompassEnabled(true); 
    mMap.getUiSettings().setTiltGesturesEnabled(true); 
    mMap.getUiSettings().setRotateGesturesEnabled(true); 
    mMap.getUiSettings().setScrollGesturesEnabled(true); 
    mMap.getUiSettings().setZoomControlsEnabled(true); 
    mMap.getUiSettings().setZoomGesturesEnabled(true); 
} 

@Override 
public boolean onMarkerClick(Marker marker) { 
    // TODO Auto-generated method stu 
    return false; 
} 

}

你不需要marker.showInfoWindow()加入到OnMarkerClickListner()interface.When單擊N到標記,然後自定義窗口創建自定義的TextView加載。 output given below

+0

還不行!我一直在檢查錯誤日誌,說「無法找到信息com.google.android.gsf.gservices提供」。該應用程序仍在運行,但這可能是問題嗎? – algorhythm

+0

你能告訴我你的manifest.xml文件還有你的佈局文件嗎? –

+0

當然,我編輯它,不適合在評論框 – algorhythm

相關問題