无法在我的CustomInfoWindow中显示我的markeroptions代码片段

时间:2019-02-06 16:44:32

标签: java android

所以基本上,我有一个用于标记的custominfowindow,我希望它显示草皮的标题以及它与当前位置的距离

所以标题很好,但我的代码片段却不正确,它显示为空白

我真的很困惑我现在应该做的事情,我看了很多教程和阅读论坛,而我却遇到了麻烦

先谢谢您!

这是我的mapsactivity.java

package com.example.turftest4;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.nfc.Tag;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.Log;

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.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.ArrayList;
import java.util.List;
import java.math.BigDecimal;

import static java.lang.Math.round;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMyLocationClickListener, GoogleMap.OnMyLocationButtonClickListener, GoogleMap.OnInfoWindowClickListener, GoogleMap.OnMarkerClickListener {

    public static GoogleMap mMap;
    public static String s;
    public static List<Marker> markerList = new ArrayList<>();
    MarkerOptions markerOptions = new MarkerOptions();


    private LocationManager locationManager;
    public LocationListener locationListener;

    private static final LatLng DreamSportsBorivali = new LatLng(19.240277, 72.847462

    );
    private static LatLng UnitedSportsFieldBorivali = new LatLng(19.243000, 72.843879

    );

    private static LatLng JBTurfBorivali = new LatLng(19.241748, 72.839832

    );

    private static LatLng OrlemLawnTurfMalad = new LatLng(19.195776, 72.837971

    );

    private Marker mDreamSportsBorivali;
    private Marker mUnitedSportsFieldBorivali;
    private Marker mJBTurfBorivali;
    private Marker mOrlemLawnTurfMalad;

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


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @SuppressLint("MissingPermission")
    @Override
    public void onMapReady(GoogleMap googleMap) {

        mMap = googleMap;
        mMap.getUiSettings().setZoomControlsEnabled(true);
        mMap.setMyLocationEnabled(true);
        mMap.setOnMyLocationButtonClickListener(this);
        mMap.setOnMyLocationClickListener(this);
        mMap.setInfoWindowAdapter(new CustomInfoWindow(getApplicationContext()));


        LatLng Mumbai = new LatLng(19.0760, 72.8777);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Mumbai,10));


        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                LatLng currentLocation = new LatLng(location.getLatitude(), location.getLongitude());


            }


            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        };


        if (Build.VERSION.SDK_INT < 23) {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        } else if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        } else {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        }




        mDreamSportsBorivali = mMap.addMarker(new MarkerOptions().position(DreamSportsBorivali).title("Dream Sports Borivali"));
        mDreamSportsBorivali.setTag(0);
        markerList.add(mDreamSportsBorivali);

        mUnitedSportsFieldBorivali = mMap.addMarker(new MarkerOptions().position(UnitedSportsFieldBorivali).title("United Sports Field Borivali"));
        mUnitedSportsFieldBorivali.setTag(0);
        markerList.add(mUnitedSportsFieldBorivali);

        mJBTurfBorivali = mMap.addMarker(new MarkerOptions().position(JBTurfBorivali).title("JB Turf Borivali"));
        mJBTurfBorivali.setTag(0);
        markerList.add(mJBTurfBorivali);

        mOrlemLawnTurfMalad = mMap.addMarker(new MarkerOptions().position(OrlemLawnTurfMalad).title("Orlem Lawn Turf Malad"));
        mOrlemLawnTurfMalad.setTag(0);
        markerList.add(mOrlemLawnTurfMalad);


        /*for(int i = 0 ; i < markerList.size() ; i++)
        {
            Location startLoc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            Location endLocation = new Location(LocationManager.GPS_PROVIDER);
            LatLng latlng = new LatLng(markerList.get(i).getPosition().latitude, markerList.get(i).getPosition().longitude);

            endLocation.setLatitude(latlng.latitude);
            endLocation.setLongitude(latlng.longitude);
            double distance = round(((startLoc.distanceTo(endLocation))/1000)*100.0)/100.0;
            s[i] = String.valueOf(distance);
            mMap.addMarker(new MarkerOptions().position(latlng).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)));
            Log.d(s[i],"kms");

        }*/
        for(Marker m : markerList)
        {
            Location startLoc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            Location endLocation = new Location(LocationManager.GPS_PROVIDER);
            LatLng latlng = new LatLng(m.getPosition().latitude, m.getPosition().longitude);

            endLocation.setLatitude(latlng.latitude);
            endLocation.setLongitude(latlng.longitude);
            double distance = round(((startLoc.distanceTo(endLocation))/1000)*100.0)/100.0;
            s = String.valueOf(distance);
            Log.d(s,"kms");
            markerOptions.position(latlng).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)).snippet("Distance : "+s);
            Marker marker = mMap.addMarker(markerOptions);
            marker.setTag(m);
        }







        // Add a marker in Sydney and move the camera
        //LatLng sydney = new LatLng(-34, 151);
        //mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        //mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED)
            if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED)
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }

    @Override
    public void onMyLocationClick(@NonNull Location location) {

    }

    @Override
    public boolean onMyLocationButtonClick() {
        return false;
    }

    @Override
    public void onInfoWindowClick(Marker marker) {

    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        return false;
    }
}

这是我的custominfowindow

package com.example.turftest4;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.Marker;

import org.w3c.dom.Text;

public class CustomInfoWindow extends MapsActivity implements GoogleMap.InfoWindowAdapter {
    private View view;
    private LayoutInflater layoutInflater;
    private Context context;

    public CustomInfoWindow(Context context) {
        this.context = context;
        layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.custom_info_window, null);
    }

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

    @Override
    public View getInfoContents(Marker marker) {
        TextView title = (TextView) view.findViewById(R.id.turftitle);
        title.setText(marker.getTitle());
        TextView dist = (TextView) view.findViewById(R.id.distance);
        dist.setText(marker.getSnippet());
        return view;
    }
}

0 个答案:

没有答案