簡體   English   中英

如何為Google地圖創建自定義信息窗口?

[英]How to create a custom info window for google maps?

我正在嘗試創建自己的信息窗口 ,但是在運行它時出現以下錯誤

錯誤:找不到符號變量LatLng

錯誤:找不到符號方法getPosition()

錯誤:無法從靜態上下文引用非靜態變量緯度

錯誤:無法從靜態上下文引用非靜態變量經度

錯誤:在atLng找不到符號類

錯誤:任務':app:compileDebugJavaWithJavac'的執行失敗。 編譯失敗; 有關詳細信息,請參見編譯器錯誤輸出。

這是我正在使用的代碼:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.widget.TextView;
import android.view.View;




public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    private GoogleMap mMap1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap1 = googleMap;
        if(mMap1 != null){
            mMap1.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter(){

            @Override
            public View getInfoWindow(Marker marker) {
                  return null;
            }

            @Override
            public View getInfoContents(Marker marker) {
                View v = getLayoutInflater().inflate(R.layout.info_window, null);
                TextView tvLocality = (TextView) v.findViewById(R.id.tv_locality);
                TextView tvLat = (TextView) v.findViewById(R.id.tv_lat);
                TextView tvLng = (TextView) v.findViewById(R.id.tv_lng);
                TextView tvSnippet = (TextView) v.findViewById(R.id.tv_snippet);

               LatLng = mMap1.getPosition();
                tvLocality.setText(marker.getTitle());
                tvLat.setText("Latitudine: " + LatLng.latitude);
                tvLng.setText("Longitudine: " + LatLng.longitude);
                tvSnippet.setText(marker.getSnippet());

                return v;
            }
            });


            }


          atLng biss = new LatLng(45.758035, 21.227514);
        mMap1.addMarker(new MarkerOptions()
                .position(biserica)
                .title("Church")
                .snippet("gafhha")
                .icon(BitmapDescriptorFactory. fromResource(R.drawable.marker)));
        mMap1.moveCamera(CameraUpdateFactory.newLatLngZoom(biss, 14));
 }
}

和info_window.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/marker"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_locality"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textSize="20sp"/>

        <TextView
            android:id="@+id/tv_lat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"/>

        <TextView
            android:id="@+id/tv_lng"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"/>

        <TextView
            android:id="@+id/tv_snippet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"/>

    </LinearLayout>

</LinearLayout>

有人知道怎么了嗎?

謝謝。

您的代碼:

LatLng = mMap1.getPosition();
            tvLocality.setText(marker.getTitle());
            tvLat.setText("Latitudine: " + LatLng.latitude);
            tvLng.setText("Longitudine: " + LatLng.longitude);
            tvSnippet.setText(marker.getSnippet());

你做錯了什么

沒有為LatLng聲明變量

應該是LatLng latlng = ....

mMap1將沒有getposition()

您應該添加marker.getPosition();

與LatLng.latitude相同,您必須使用latlng.latitude

修改后的代碼

 LatLng latlng= marker.getPosition();
                tvLocality.setText(marker.getTitle());
                tvLat.setText("Latitudine: " + latlng.latitude);
                tvLng.setText("Longitudine: " + latlng.longitude);
                tvSnippet.setText(marker.getSnippet());

其他錯誤

atLng biss = new LatLng(45.758035, 21.227514);
    mMap1.addMarker(new MarkerOptions()
            .position(biserica)
            .title("Church")
            .snippet("gafhha")
            .icon(BitmapDescriptorFactory. fromResource(R.drawable.marker)));
    mMap1.moveCamera(CameraUpdateFactory.newLatLngZoom(biss, 14));

更改為

atLng更改為LatLng

biserica更改為biss

LatLng biss = new LatLng(45.758035, 21.227514);
    mMap1.addMarker(new MarkerOptions()
            .position(biss)
            .title("Church")
            .snippet("gafhha")
            .icon(BitmapDescriptorFactory. fromResource(R.drawable.marker)));
    mMap1.moveCamera(CameraUpdateFactory.newLatLngZoom(biss, 14));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM