Code example for GoogleMap

Methods: addMarker, addPolygon, animateCamera, getProjection

0
 
        // Getting user input location 
        String location = etLocation.getText().toString();
 
        if(location!=null && !location.equals("")) {
            new GeocoderTask().execute(location);
        } 
    } 
	 
	@Override 
	public void onMapLongClick(LatLng point) {		
		 
		// If a single location is selected, put a marker 
		if (singleLocation) {
			if (singleMarker != null) singleMarker.remove();
			singleMarker = map.addMarker(new MarkerOptions().position(point));
			gps1.setLatitude(point.latitude); gps1.setLongitude(point.longitude);
			 
		}  
		 
		// If an area is selected, draw the rectangle 
		else { 
			// Get boundaries of the screen from the projection 
			VisibleRegion bounds = map.getProjection().getVisibleRegion();
				 
			drawRect(bounds.farLeft, bounds.farRight, bounds.nearRight, bounds.nearLeft);
			map.animateCamera(CameraUpdateFactory.zoomBy(ZOOM_OUT));
		} 
		 
		// Show a toast 
        Toast.makeText(this, "Location updated", Toast.LENGTH_SHORT).show();
	} 
	 
	private void drawRect(LatLng c1, LatLng c2, LatLng c3, LatLng c4) {
 
		// Remove previously drawn rect 
		if (rect != null) rect.remove();
		 
		// Add rectangle to the map 
		rect = map.addPolygon(new PolygonOptions()
		    .add(c1,c2,c3,c4) // 4 corners, ccw
		    .strokeWidth(5)
		    .strokeColor(Color.GREEN)
		    .fillColor(0x6600ff00));
		 
		// Set coordinates that will be passed back to the group creation activity 
		gps1.setLatitude(c1.latitude); gps1.setLongitude(c1.longitude);
		gps2.setLatitude(c3.latitude); gps2.setLongitude(c3.longitude);
 
		// Top left marker 
		if (m1 != null) m1.remove();
		m1 = map.addMarker(new MarkerOptions()
			    	.position(c1)
			    	.draggable(true)
			    	.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
		 
		// Top right marker 
		if (m2 != null) m2.remove();