【问题标题】:Displaying custom text in a map snippet, Android在地图片段中显示自定义文本,Android
【发布时间】:2014-01-05 23:28:46
【问题描述】:

好吧,我已经一整天都在做这个,我放弃了自己尝试得到这个!

我已经声明了地图和位置

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;
            }
    });

谁能指出我哪里出错了?地图加载,您可以单击标记以获取 sn-p,但未显示自定义信息

编辑以包含清单

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

【问题讨论】:

    标签: java android xml maps code-snippets


    【解决方案1】:

    以下代码可能对您有所帮助。

    公共类 Stacky 扩展 FragmentActivity 实现 OnMarkerClickListener {

    静态最终 LatLng SECC = new LatLng(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() 界面中。当您单击 n 进行标记时,会创建自定义窗口并使用您的自定义文本视图加载。

    【讨论】:

    • 还是不行!我一直在检查错误日志,显示“无法找到提供信息 com.google.android.gsf.gservices”。该应用程序仍在运行,但这可能是问题吗?
    • 你能给我看看你的 manifest.xml 文件和你的布局文件吗?
    • 当然,我编辑的,不适合评论框
    • 很抱歉,它看起来很糟糕似乎无法像代码一样将 xml 文件正确显示在此显示
    • 并添加您的 layout.xml 文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多