2013-09-30 111 views
0

嗨,我有問題如何實現信息窗口上點擊標記? 問題是我在地圖上有很多標記,如果單擊信息窗口,每個標記都有另一個活動。 enter code hereAndroid地圖infowindow上點擊標記

繼承人的示例

  1. 標記一個----->如果點擊信息窗口標記之一,然後ActivityOne將顯示
  2. 標記兩個----->如果點擊信息窗口標記2然後ActivityTwo將顯示

我已經嘗試了許多代碼,但沒有解決

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

回答

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 
    } 
} 
+1

沒有標記,但信息窗口從標記 – kucluk

+0

不管是什麼,你應該保持一個參考標記之一,並以標誌二:當你添加它們。然後在'onMarkerClick'或'onInfoWindowClick',你可以檢查'if(m == m1)'或'if(m == m2)' –

+0

仍然不能解決它 – kucluk

5

這是完美的工作:

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); 
      } 
     } 
    }); 
+0

我無法解決這個問題 – kucluk

0

好主意是設置標記標題,然後在監聽器基於標題做動作

marker.getTitle() 
0
Mymap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() { 
      @Override 
      public void onInfoWindowClick(Marker marker) { 
       Intent intent = new Intent(MapActivity.this,OtherActivity.class); 
       startActivity(intent); 


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

      } 
     } 

希望這將幫助你:-)

相關問題