【问题标题】:Android map infowindow on click marker点击标记上的 Android 地图信息窗口
【发布时间】:2013-10-06 10:10:12
【问题描述】:

您好,我有疑问如何在点击标记时实现信息窗口? 问题是我在地图上有很多标记,如果单击信息窗口,每个标记都有另一个活动。enter code here

这个例子

  1. 标记一---->如果单击信息窗口标记一,则 ActivityOne 将显示
  2. 标记二----->如果单击信息窗口标记二,则 ActivityTwo 将显示

我尝试了很多代码但没有解决

       Intent intent = new Intent(ThisActivity.this,OneActivity.class);
          startActivity(intent);
        }
   });

【问题讨论】:

    标签: android android-activity marker infowindow


    【解决方案1】:

    这是完美的工作:

     mapview.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
               if(marker==marler1){
                 Intent intent = new Intent(MapActivity.this,Activity1.class);
                 startActivity(intent);
               }else(marker==marler2){
                 Intent intent = new Intent(MapActivity.this,Activity2.class);
                 startActivity(intent);
               }
            }
        });
    

    【讨论】:

      【解决方案2】:

      首先:尝试更多地寻找解决方案。

      提示:(伪代码)

      Marker m1 = map.addMarker(bla bla);
      Marker m2 = map.addMarker(bla bla);
      
      onMarkerClicked(Marker m) {
         if(m == m1) {
            //do what you want to do for marker one
         }
         else if(m == m2) {
            //do what you want to do for marker two
         }
      }
      

      【讨论】:

      • 不管是什么,添加标记一和标记二时都应该保留它们的引用。然后在onMarkerClickonInfoWindowClick,您可以检查if(m == m1)if(m==m2)
      【解决方案3】:

      好主意是设置标记标题,然后在侦听器中根据标题执行操作

      marker.getTitle()
      

      【讨论】:

        【解决方案4】:

        这是我的代码。

        首先在build.gradle中添加依赖。

            compile 'com.google.android.gms:play-services:8.4.0'
            compile 'com.google.android.gms:play-services-location:8.1.0'
            compile 'com.google.android.gms:play-services-maps:8.4.0'
        
        
                public class MapDetailsActivity extends AppCompatActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
        
                    private static final int WRITE_EXTERNAL_STORAGE_REQUEST_CODE = 1;
                    LocationRequest mLocationRequest;
                    private GoogleApiClient mGoogleApiClient;
                    LatLng latLng;
                    GoogleMap mGoogleMap;
                    SupportMapFragment mFragment;
                    Marker currLocationMarker;
                    private double longitude;
                    private double latitude;
                    private String strname;
                    private String strADD;
                    private String strLat;
                    private String strLng;
                    private BreakIterator mLatitudeText;
                    private BreakIterator mLongitudeText;
                    private MarkerOptions markerOptions;
        
                    @Override
                    protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_map);
                        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE_REQUEST_CODE
                        );
        
                        Bundle bundle = getIntent().getExtras();
                        strname = bundle.getString("Vname");
                        Log.e("444", "NAME--:" + strname);
                        strADD = bundle.getString("Address");
                        Log.e("444", "ADDRESS:--" + strADD);
        
        
                        strLat = bundle.getString("Lat");
                        Log.e("444", "LATTITUDE:--" + strLat);
                        strLng = bundle.getString("Lng");
                        Log.e("444", "LNGitudE:--" + strLng);
        
        
                        mFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
                        mFragment.getMapAsync(this);
        
                    }
        
                    @Override
                    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
                        if (grantResults.length == 0 || grantResults[0] == PackageManager.PERMISSION_DENIED) {
                            return; //permission not granted, could also optionally log an error
                        }
                        if (requestCode == WRITE_EXTERNAL_STORAGE_REQUEST_CODE) {
                            //Do whatever you needed the write permissions for
                        }
                    }
        
                    @Override
                    public void onMapReady(GoogleMap gMap) {
                        Log.e("RESDY", "^^^6");
                        mGoogleMap = gMap;
        
                        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                            // TODO: Consider calling
                            //    ActivityCompat#requestPermissions
                            // here to request the missing permissions, and then overriding
                            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                            //                                          int[] grantResults)
                            // to handle the case where the user grants the permission. See the documentation
                            // for ActivityCompat#requestPermissions for more details.
                            return;
                        }
                        gMap.setMyLocationEnabled(true);//used to enable location layer which will allow a user to interact with current user location.
                        gMap.getUiSettings().setZoomControlsEnabled(true);
                        gMap.getUiSettings().setRotateGesturesEnabled(true);
                        gMap.getUiSettings().setScrollGesturesEnabled(true);
                        gMap.getUiSettings().setTiltGesturesEnabled(true);
                        buildGoogleApiClient();
                        mGoogleApiClient.connect();
                    }
        
                    protected synchronized void buildGoogleApiClient() {
                //        GoogleApiClient.Builder is used to configure client
                //        Toast.makeText(this, "buildGoogleApiClient", Toast.LENGTH_SHORT).show();
                        mGoogleApiClient = new GoogleApiClient.Builder(this)
                                .addConnectionCallbacks(this)//called when client connected or disconnected.
                                .addOnConnectionFailedListener(this)//failed attempt of connect client to service.
                                .addApi(LocationServices.API)//adds the LocationServices API endpoint from Google Play Services.
                                .build();
                    }
        
                    @Override
                    public void onConnected(Bundle bundle) {
                //        Toast.makeText(this, "onConnected", Toast.LENGTH_SHORT).show();
        
        
                        mLocationRequest = new LocationRequest();
                        mLocationRequest.setInterval(5000); //5 seconds
                        mLocationRequest.setFastestInterval(3000); //3 seconds
                        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
                        //mLocationRequest.setSmallestDisplacement(0.1F); //1/10 meter
                        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                            // TODO: Consider calling
                            //    ActivityCompat#requestPermissions
                            // here to request the missing permissions, and then overriding
                            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                            //                                          int[] grantResults)
                            // to handle the case where the user grants the permission. See the documentation
                            // for ActivityCompat#requestPermissions for more details.
                            return;
                        }
                        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
                    }
        
                    @Override
                    public void onConnectionSuspended(int i) {
                        Toast.makeText(this, "onConnectionSuspended", Toast.LENGTH_SHORT).show();
                    }
        
                    @Override
                    public void onConnectionFailed(ConnectionResult connectionResult) {
                        Toast.makeText(this, "onConnectionFailed", Toast.LENGTH_SHORT).show();
                    }
        
                    @Override
                    public void onLocationChanged(Location location) {
                        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                            // TODO: Consider calling
                            //    ActivityCompat#requestPermissions
                            // here to request the missing permissions, and then overriding
                            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                            //                                          int[] grantResults)
                            // to handle the case where the user grants the permission. See the documentation
                            // for ActivityCompat#requestPermissions for more details.
                            return;
                        }
                //        Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        
                        mGoogleMap.clear();
                        if (currLocationMarker != null) {
                            currLocationMarker.remove();
                        }
        
                        longitude = Double.parseDouble(strLng);
                        latitude = Double.parseDouble(strLat);
                        latLng = new LatLng(latitude, longitude);
                        mGoogleMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());
                        markerOptions = new MarkerOptions();
                        markerOptions.position(latLng);
                        markerOptions.title("" + strname);
                        markerOptions.snippet("" + strADD);
                        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
                        currLocationMarker = mGoogleMap.addMarker(markerOptions);
        
                        //zoom with animation:
                //        CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latitude, longitude)).zoom(13).bearing(90).build();
                //        mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
                  mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 11));
        
             LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        
        
                    }
        
                    public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
        
                        private View view;
        
                        public CustomInfoWindowAdapter() {
                            view = getLayoutInflater().inflate(R.layout.custom_info_window, null);
                        }
        
                        @Override
                        public View getInfoContents(Marker marker) {
        
                            if (marker != null
                                    && marker.isInfoWindowShown()) {
                                marker.hideInfoWindow();
                                marker.showInfoWindow();
                            }
                            return null;
                        }
        
                        @Override
                        public View getInfoWindow(Marker marker) {
        
                            final String title = marker.getTitle();
                            final TextView titleUi = ((TextView) view.findViewById(R.id.title));
                            if (title != null) {
                                titleUi.setText(title);
                            } else {
                                titleUi.setText("");
                            }
        
                            final String snippet = marker.getSnippet();
                            final TextView snippetUi = ((TextView) view
                                    .findViewById(R.id.snippet));
                            if (snippet != null) {
                                snippetUi.setText(snippet);
                            } else {
                                snippetUi.setText("");
                            }
        
                            return view;
                        }
        
                    }
                }
        

        希望这会对你有所帮助:-)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-07-26
          • 1970-01-01
          • 1970-01-01
          • 2023-03-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多