Override Default Location on Android Phone using Java - java

I have followed a 24 hours textbook and created an Android app that is supposed to launch Google maps and navigate to a specific location based on latitude and longitude coordinates hard coded into the app. However, when I launch the app and run the code it opens Google maps or Google Earth and always defaults to my current location. How do I prevent it from doing this?
I am trying to get my app to go to a specific latitude and longitude, but when it launches Google Maps or Google Earth it always defaults to my current location. Java Code is as follows:
Button mapButton = (Button) findViewById(R.id.button2);
mapButton.setOnClickListener(new View.OnClickListener() {
#override
public void onClick(View view) {
String geoURI = "geo:37.422,-122.0847z=23";
Uri geo = Uri.parse(geoURI);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, geo);
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
How do I get it to override the default location and go to the coordinates I have hard coded above?

Looking at the documentation, the line:
String geoURI = "geo:37.422,-122.0847z=23";
Appears to be invalid syntax, but might be wanting to zoom. Try:
String geoURI = "geo:37.422,-122.0847?z=23";
Zoom level only goes up to 21, however, so also try:
String geoURI = "geo:37.422,-122.0847?z=21";
If neither work, use a basic string without any zoom:
String geoURI = "geo:37.422,-122.0847";

Try this:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:<lat>,<long>?q=<lat>,<long>(Label+Name)"));
startActivity(intent);
You can omit (Label+Name) if you don't want a label, and it will choose one randomly based on the nearest street or other thing it thinks relevant.

You can create LatLng object and tell google map to load the location. Code to do that will be something like this:
double lat=26.52719;
double lng=88.72448;
LatLng myLocation = new LatLng(lat,lng);
if(this.googleMap !=null){
this.googleMap.addMarker(new MarkerOptions().position(myLocation).title("Your current Location").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
this.googleMap.moveCamera(CameraUpdateFactory.newLatLng(myLocation));
}
Remember you should apply this in onMapReady callback and to turn off your current location placing in google map you can do
this.googleMap.setMyLocationEnabled(true);

Related

How to get name of polygon or polyline if my location inthere ? (OSMDroid)

Im working with OSMDroid to make map offline in Android Studio.
this is my code to create Polygon :
polygon = new Polygon();
polygon.setPoints(list_polygon);
polygon.setFillColor(Color.parseColor("#1D566E"));
polygon.setTitle(name_map);
polygon.getOutlinePaint().setColor(polygon.getFillPaint().getColor());
map.getOverlays().add(polygon);
this code is to create line :
line = new Polyline();
line.setPoints(list_line);
line.setGeodesic(true);
line.setColor(Color.parseColor("#E33E5A"));
line.getOutlinePaint().setStrokeWidth(30f);
line.setWidth(15f);
line.getPaint().setStrokeCap(Paint.Cap.ROUND);
map.getOverlays().add(line);
and this code is to get my location :
myLocation = new MyLocationNewOverlay(map);
myLocation.enableFollowLocation();
myLocation.setDirectionArrow(icTruk, icTruk);
myLocation.enableMyLocation();
myLocation.setDrawAccuracyEnabled(true);
map.getOverlays().add(myLocation);
I have done to create polygon and polyline in osmdroid.
But now i want read that polygon or polyline if mylocation inthere ?
How to make it posible ?
You could do the following to get the current location and then check if it is close to the Polyline.
MyLocationNewOverlay myLocation= new MyLocationNewOverlay(mapView) {
#Override
public void onLocationChanged(Location location, IMyLocationProvider source) {
super.onLocationChanger(location, source);
// Turn the current location into a GeoPoint
GeoPoint currentPoint = new GeoPoint(location.getLatitude(), location.getLongitude);
// Set tolerance for isCloseTo
// Might need to play around with this value
// and see which fits best for your needs
double tolerance = 5.0;
// Check if location is close to Polyline
if (line.isCloseTo(currentPoint, tolerance, map)) {
// Do here what you want to do,
// when location is close to Polyline
}
}
}
Checking, if the location/GeoPoint is within a given polygon is a bit more work, since the only integrated method for that is based on MotionEvent rather than GeoPoint and only returns a correct value in a very specific scenario, see this for reference. But there are some answers out there, which might be usable for your need, like this one.
References:
Get the current location
OSMDroid Geopoint class
Android Location class
OSMDroid Polyline.isCloseTo
OSMDroid Polygon.contains
One way to check if point is in a polygon

I want to add GeoJson to mapbox mapsnapshot

I have been trying to find an efficient way to show multiple views of a mapbox map, and I have landed on the mapbox snapshotter. However I have had little luck when trying to add a geojson string of coordinates to the snapshot.
I used the below code:
int width = myContext.getResources().getDisplayMetrics().widthPixels;
int height = (int) convertDpToPixel(200f, myContext);
MapSnapshotter.Options snapShopOptions = new MapSnapshotter.Options(width, height);
LatLngBounds bounds = new LatLngBounds.Builder()
.include(post.getPath().get(0))
.include(post.getPath().get(post.getPath().size() - 1))
.build();
snapShopOptions.withRegion(bounds);
snapShopOptions.withStyle(myContext.getString(R.string.outdoor_style));
mapSnapshotter = new MapSnapshotter(myContext, snapShopOptions);
mapSnapshotter.setStyleJson(String.valueOf(new Style.Builder()
.fromUri("mapbox://styles/mapbox/outdoors-v11")));
mapSnapshotter.start(new MapSnapshotter.SnapshotReadyCallback() {
#Override
public void onSnapshotReady(MapSnapshot snapshot) {
Bitmap bitmapImage = snapshot.getBitmap();
Glide.with(myContext).load(bitmapImage).into(holder.mapSnapShot);
}
});
I have tried initializing a MapboxMap object and adding the geojson to that feature and then using the .getProjection(), but it still does not show the route.
Can anyone help me solve this issue??
I ended up contacting Mapbox support and as of now the feature is not supported. Their exact quote was "Mapbox's MapSnapshotter does not support your use case, but it will once runtime styling is supported by the snapshotter."
Support then pointed me to this post Android Convert current screen to bitmap.
Hopefully if anyone was struggling similarly to me this will help save some time!

Place marker at latitude and longitude of centre of map

Had an issue where I needed to place a marker at the exact centre of my map screen and looked at a lot of questions and answers that were not exactly what I wanted to do. The app allows the user to move map about and then once crosshairs that are exactly in the centre of the map are at the desired location, you tap a button and a marker appears at that location.
map.getProjection().fromScreenLocation almost met my needs but I did not have the coordinates so could not implement that solution.
So my answer hopefully will help someone in the same situation.
So my soltuion was to use the getCameraPosition method as follows. And its fired from the button R.id.addMarker in my xml.
findViewById(R.id.addMarker).setOnClickListener(
new View.OnClickListener(){
#Override
public void onClick(View view) {
if(myMap != null){
double myLat = myMap.getCameraPosition().target.latitude;
double myLng = myMap.getCameraPosition().target.longitude;
LatLng markerPoint = new LatLng(myLat, myLng);
myMap.addMarker(new MarkerOptions().position(markerPoint)).setTitle("next marker");
}
}
}
);
Hope it helps someone in the same predicament.
I don't understand why fromScreenLocation doesn't suit your needs...
If you pass the map's width and the map's height divided by two as it's parameters, this function will return you a LatLng object giving you the corresponding location in terms of latitude and longitude. After that, it is easy to place a Markerat this position.

How to show longitude and altitude on map in Android?

i have a place coordinate, i mean altitude and longitude , and i want to show this place with google map in android application, so how can i use this altitude and longitude to pass in a function and show that place in the app, i don't need to show current place , i have this longitudes and altitudes from before,and i save theme as a string in my application , is there any function or layout to show this place with this coordinates in application ? or i have to use some different method to show this places on map?
private GoogleMap mMap;
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.addMarker(new MarkerOptions()
.position(new LatLng(10, 10))
.title("Hello world"));
You could try:
String urlAddress = "http://maps.google.com/maps?q="+ lattitude string +"," + longitude_string +"("+ "Hello location" + ")&iwloc=A&hl=es";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlAddress));
startActivity(intent);
This will open google maps with whatever coordinates you provide.

How to place 2 TextView at same place with if else condition in Android

In my app I am showing the latitude and longitude of a place. I have place a separate layout for those two. Now the problem is if there is no network I want to show an error message at the same place, i.e. within the same layout.
If there is network I want to show the TextView1, the values of latitude and longitude else I want to show the TextView2. Is it possible can anyone explain me this with some sample codes.
Is there any other simple way to do this?
Is there any API to check network?
You can use the same TextView:
boolean condition;
TextView tv = (TextView) findViewById(R.id.your_text_view);
if (condition) {
tv.setText("something");
}
else
{
tv.setText("something else");
}
You can do it in java source files. I believe it would be
Textview v = findViewById(your view's id);
if(no network)
{
v.setText("what ever you want");
}
else
{
v.setText("you longitude and lattitude text would go here");
}

Categories