0% found this document useful (0 votes)
2 views

df

The document outlines the implementation of a MapActivity in an Android application using the Mappls SDK. It includes features such as initializing the map, adding markers, drawing a polyline between two points, and setting up a location component for user tracking. Additionally, it manages camera positioning and handles location updates with appropriate callbacks.

Uploaded by

Atharva Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

df

The document outlines the implementation of a MapActivity in an Android application using the Mappls SDK. It includes features such as initializing the map, adding markers, drawing a polyline between two points, and setting up a location component for user tracking. Additionally, it manages camera positioning and handles location updates with appropriate callbacks.

Uploaded by

Atharva Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import android.location.

Location;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import com.mappls.sdk.map.MapplsMap;
import com.mappls.sdk.map.model.LatLng;
import com.mappls.sdk.map.model.MarkerOptions;
import com.mappls.sdk.map.model.PolylineOptions;
import com.mappls.sdk.location.LocationComponent;
import com.mappls.sdk.location.LocationComponentOptions;
import com.mappls.sdk.location.LocationEngine;
import com.mappls.sdk.location.LocationEngineRequest;
import com.mappls.sdk.location.LocationEngineCallback;
import com.mappls.sdk.location.LocationEngineResult;
import com.mappls.sdk.map.CameraPosition;
import com.mappls.sdk.map.CameraUpdateFactory;

public class MapActivity extends AppCompatActivity {

private MapplsMap mapplsMap;


private LocationComponent locationComponent;
private LocationEngine locationEngine;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);

// Initialize map and location components


initializeMapAndLocation();

// Add markers
LatLng firstLatLng = new LatLng(28.0, 77.0);
LatLng secondLatLng = new LatLng(28.67, 77.65);

addMarkers(firstLatLng, secondLatLng);

// Draw a polyline between two points


addPolyline(firstLatLng, secondLatLng);

// Set up the location component for user tracking


setUpLocationComponent();

// Set camera position with tilt and zoom


setCameraPosition();
}

private void initializeMapAndLocation() {


// Assuming mapplsMap is initialized and ready to use
mapplsMap.addOnMapClickListener(point -> {
String message = String.format("User clicked at: %s",
point.toString());
Toast.makeText(MapActivity.this, message, Toast.LENGTH_LONG).show();
return false; // Allow map click listener
});
// Set up the location engine for real-time updates
locationEngine = locationComponent.getLocationEngine();
}

private void addMarkers(LatLng firstLatLng, LatLng secondLatLng) {


// Adding first marker
MarkerOptions markerOptions1 = new MarkerOptions()
.position(firstLatLng)
.title("First Marker")
.snippet("This is the first marker");
mapplsMap.addMarker(markerOptions1);

// Adding second marker


MarkerOptions markerOptions2 = new MarkerOptions()
.position(secondLatLng)
.title("Second Marker")
.snippet("This is the second marker");
mapplsMap.addMarker(markerOptions2);
}

private void addPolyline(LatLng firstLatLng, LatLng secondLatLng) {


// Add a polyline between the two markers
mapplsMap.addPolyline(new PolylineOptions()
.add(firstLatLng, secondLatLng) // Add LatLng points for the
polyline
.color(ContextCompat.getColor(this, R.color.colorPrimary)) // Set
polyline color
.width(5)); // Set polyline width
}

private void setUpLocationComponent() {


// Location component options
LocationComponentOptions options = LocationComponentOptions.builder(this)
.trackingGesturesManagement(true)
.accuracyColor(ContextCompat.getColor(this, R.color.colorAccent))
.build();

// Activate location component


locationComponent = mapplsMap.getLocationComponent();
locationComponent.activateLocationComponent(this, options);

// Enable location component


locationComponent.setLocationComponentEnabled(true);

// Set up location updates


LocationEngineRequest request = new LocationEngineRequest.Builder(1000)
.setPriority(LocationEngineRequest.PRIORITY_HIGH_ACCURACY)
.build();

LocationEngineCallback<LocationEngineResult> locationEngineCallback = new


LocationEngineCallback<LocationEngineResult>() {
@Override
public void onSuccess(LocationEngineResult result) {
if (result.getLastLocation() != null) {
Location location = result.getLastLocation();
Log.d("Location Update", "User's location: " +
location.toString());
}
}
@Override
public void onFailure(@NonNull Exception e) {
Log.e("Location Update", "Failed to get location", e);
}
};

locationEngine.requestLocationUpdates(request, locationEngineCallback,
getMainLooper());
}

private void setCameraPosition() {


// Setting camera position with zoom and tilt
CameraPosition position = new CameraPosition.Builder()
.target(new LatLng(28.0, 77.0)) // Focus the camera on the first
marker
.zoom(12) // Set zoom level
.tilt(45) // Set tilt (for a 3D effect)
.build();

mapplsMap.setCameraPosition(position);
}

@Override
protected void onDestroy() {
super.onDestroy();
// Remove location updates to prevent memory leaks
if (locationEngine != null) {
locationEngine.removeLocationUpdates(locationEngineCallback);
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy