如何为谷歌地图创建自定义信息窗口?

时间:2017-06-15 11:35:16

标签: java android

尝试创建自己的信息窗口,但运行时我遇到了这些错误

  

错误:找不到符号变量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>

有谁知道什么是错的?

感谢。

1 个答案:

答案 0 :(得分:2)

您的代码:

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