0

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

继承人的例子

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

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

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

4 回答 4

9

这是完美的工作:

 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);
           }
        }
    });
于 2013-10-01T09:14:20.740 回答
0

第一:尝试更多地寻找解决方案。

提示:(伪代码)

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
   }
}
于 2013-09-30T14:04:20.820 回答
0

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

marker.getTitle()
于 2015-05-27T08:39:47.383 回答
0

这是我的代码。

首先在 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;
                }

            }
        }

希望对你有帮助 :-)

于 2017-11-28T05:34:53.600 回答