Detect the coordinates on the screen - android

I want to detect the coordinates of a map are touched on the screen with a finger. The problem is when I touch the screen with a finger the coordinates are not displayed with a toast.
I am using Google Maps v2. My code:
#Override
public boolean onTouchEvent(MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_POINTER_DOWN)
{
CrearLugar();
}
return super.onTouchEvent(event);
}
public void CrearLugar()
{
final GoogleMap mMap=null;
mMap.setOnMapClickListener(new OnMapClickListener() {
public void onMapClick(LatLng point) {
Projection proj=mMap.getProjection();
Point coord = proj.toScreenLocation(point);
Toast.makeText(
MapaLugaresActivity.this,
"Click\n" +
"Lat: " + point.latitude + "\n" +
"Lng: " + point.longitude + "\n" +
"X: " + coord.x + " - Y: " + coord.y,
Toast.LENGTH_SHORT).show();
}
});
}
Everything is correct except when I touch the screen with a finger. When I do it, I want to display the coordinates of the place touched, but I can't watch anything, only the map.
What's wrong?
Thanks.

The click listener has to be set in any of the creators of the activity (onCreate, onResume, onPause, ...). Note that you are setting the listener to (listen) for changes.
You can still give some use to the onTouch overriden method. It'll help to get the absolute coordinates where the user clicked in your screen, but it's completely independent from the map. (btw, what do you need the absoluta coordinates for?).
Try that:
#Override
public boolean onTouchEvent(MotionEvent event)
{
Toast.makeText(
MapaLugaresActivity.this,
"Click\n" +
"X: " + event.getX() + " - Y: " + event.getY(),
Toast.LENGTH_SHORT).show();
return super.onTouchEvent(event);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate view
// Inflate map view
mMap.setOnMapClickListener(new OnMapClickListener() {
public void onMapClick(LatLng point) {
Projection proj=mMap.getProjection();
Point coord = proj.toScreenLocation(point);
Toast.makeText(
MapaLugaresActivity.this,
"Click\n" +
"Lat: " + point.latitude + "\n" +
"Lng: " + point.longitude + "\n",
Toast.LENGTH_SHORT).show();
}
});
}

Related

Getting some weird strings while i was trying to get location name using marker on touch event on maps

I'm trying to make an app to get the location name where the marker is placed on the map in android. When I used marker.position() function it gave me a weird string. Starts with, com.google.android.gms.maps.model.MarkerOptions#28f817d
I tried to use geocode but didn't work for me. I might be using it in an improper way.
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng point) {
mMap.clear();
MarkerOptions marker = new MarkerOptions();
mMap.addMarker(marker.position(point)
.draggable(true)
.title(String.valueOf(marker.position(point)))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.snippet(point.latitude + "," + point.longitude));
}
});
}
I wish that I can get the location name on the title.
Thats becouse marker.position(point)) returns an object of class LatLng and is not an String itself. You could use any of its properties (latitude & longitude):
String.valueOf(marker.position(point))
to
"" + marker.position(point).latitude + marker.position(point).longitude
Do something like this
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng point) {
mMap.clear();
MarkerOptions marker = new MarkerOptions();
mMap.addMarker(marker.position(point)
.draggable(true)
.title(getAddress(point.getLatitude(),point.getLongitude()))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.snippet(point.latitude + "," + point.longitude));
}
});
}
write a function called getAddress like this
public string getAddress(double lat, double lng) {
Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Address obj = addresses.get(0);
String add = obj.getAddressLine(0);
add = add + "\n" + obj.getCountryName();
add = add + "\n" + obj.getCountryCode();
add = add + "\n" + obj.getAdminArea();
add = add + "\n" + obj.getPostalCode();
add = add + "\n" + obj.getSubAdminArea();
add = add + "\n" + obj.getLocality();
add = add + "\n" + obj.getSubThoroughfare();
Log.v("IGA", "Address" + add);
// Toast.makeText(this, "Address=>" + add,
// Toast.LENGTH_SHORT).show();
// TennisAppActivity.showDialog(add);
return add;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
return e;
}
}

Use AccessibilityNodeInfo to turn switch on and off

Okay, so I want to know if it is possible to use AccessibilityService/AccessibilityNodeInfo to turn an android.widget.Switch on/off? Here is my current code and it works to click the text on the screen, but it does not turn a switch on/off.
public void onAccessibilityEvent(AccessibilityEvent event) {
AccessibilityNodeInfo source = event.getSource();
if (source == null) {
return;
}
Log.v(TAG, "Event happened");
processUI(source);
}
And of course, the processUI does everything like compares package, text, etc. and sends it all to clickScreen where it actually clicks the screen.
protected void clickScreen(AccessibilityNodeInfo source, final String text, final String type, final int length)
{
Log.v(TAG, "Clicking: " + source + " / " + text + " / " + type + " / " + length);
if (text == null)
{
Log.v(TAG, "Text is NULL");
return;
}
List<AccessibilityNodeInfo> list = source.findAccessibilityNodeInfosByText(text);
for (AccessibilityNodeInfo node : list) {
Log.i(TAG, "SHOULD BE CLICKING: " + node);
node.performAction(4);
node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
list = source
.findAccessibilityNodeInfosByText(text);
for (AccessibilityNodeInfo node : list) {
Log.i(TAG, "MIGHT BE CLICKING: " + node);
node.performAction(4);
node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
}

Position indicator in HERE SDK premium is not showing

Here is my simple implementation.
The MapFragment is already initialized. If I do the simulation mode the position indicator is showing but if I switched to the actual TBT navigation it is not showing where I am located.
PositionManager positioningManager;
if(positioningManager == null) {
positioningManager = PositioningManager.getInstance();
positioningManager.addListener(new WeakReference<>(positionChangedListener));
positioningManager.start(PositioningManager.LocationMethod.GPS_NETWORK);
}
positionIndicator = mMap.getPositionIndicator();
positionIndicator.setVisible(true);
positionIndicator.setAccuracyIndicatorVisible(true);
private PositioningManager.OnPositionChangedListener positionChangedListener =
new PositioningManager.OnPositionChangedListener() {
#Override
public void onPositionUpdated(PositioningManager.LocationMethod locationMethod,
GeoPosition geoPosition, boolean b) {
Log.d(TAG, "onPositionUpdated " + locationMethod.name());
Log.d(TAG, "Coordinates " +geoPosition.getCoordinate());
}
#Override
public void onPositionFixChanged(PositioningManager.LocationMethod locationMethod,
PositioningManager.LocationStatus locationStatus) {
Log.d(TAG, "onPositionFixChanged " + locationMethod.name() + " status " + locationStatus.name());
}
};

Google Maps Link Broken By Symbols

I am developing an emergency application for my school, and I had a question. I have code that gets your coordinates, and smsmanager sends them to a designated phone number. I tried to put the coordinates in a Google Maps link so the recipient can just click a link instead of inputting the coordinates into Google Maps, but when I do, the link is broken by the "=", "<" and ">" signs. I was wondering what I could do in my code to make the link work. When the recipient receives the message, it looks like this..
"http://maps.google.com/?q"=<"######">,<"######">
Everything in quotations is a link, the rest is not.
public void onClick(View v) {
try {
txtLat = (TextView) findViewById(R.id.textview1);
String messageToSend = txtLat.getText().toString();
String number = "8143664050";
SmsManager.getDefault().sendTextMessage(number, null, messageToSend, null,null);
Toast.makeText(getApplicationContext(), "Help SMS Sent",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "SMS failed!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
}
}
#Override
public void onLocationChanged(Location location) {
txtLat = (TextView) findViewById(R.id.textview1);
txtLat.setText("http://maps.google.com/?q=<" + location.getLatitude() + ">,<" + location.getLongitude() + ">");
}
try:
int zoom = 12;
txtLat.setText(
"https://www.google.com/maps/#" + location.getLatitude() + "," +
location.getLongitude() + "," + zoom + "z");
for 3d/satellite map:
txtLat.setText(
"https://www.google.com/maps/#" + location.getLatitude() + "," +
location.getLongitude() + ",15000m/data=!3m1!1e3");
The address should be automatically updated when you are using Google Maps, try around and see how it changes.

aChartEngine, GraphicalView OnClickListener not working

I'm new to android and am using aChartEngine to create a bar chart. I want to capture the x and y values when a user clicks on the chart. I have looked at the demos from aChartEngine and have my chart creating fine. However the onClickListner does not work when I click on the graphy. I have defined the listener in the OnResume method but it doesn't work. Any ideas would be greatly appreciated. Am I missing something here?
Here is my OnResume method which is taken from XYChartBuilder
#Override
protected void onResume() {
super.onResume();
Toast.makeText(Clickable2.this, "In OnResume", Toast.LENGTH_SHORT).show();
if (mChartView == null) {
Toast.makeText(Clickable2.this, "mChartView is null", Toast.LENGTH_SHORT).show();
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
mChartView = ChartFactory.getBarChartView(this,mDatasetMethod(titles,x,values),renderer,Type.DEFAULT);
renderer.setClickEnabled(true);
renderer.setSelectableBuffer(100);
//OnClickListener
mChartView.setOnClickListener(new View.OnClickListener() {
// #Override
public void onClick(View v) {
Toast.makeText(Clickable2.this, "ON CLICK", Toast.LENGTH_SHORT).show();
SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
double[] xy = mChartView.toRealPoint(0);
if (seriesSelection == null) {
Toast.makeText(Clickable2.this, "No chart element was clicked", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(
Clickable2.this,
"Chart element in series index " + seriesSelection.getSeriesIndex()
+ " data point index " + seriesSelection.getPointIndex() + " was clicked"
+ " closest point value X=" + seriesSelection.getXValue() + ", Y=" + seriesSelection.getValue()
+ " clicked point value X=" + (float) xy[0] + ", Y=" + (float) xy[1], Toast.LENGTH_SHORT).show();
}
}
});
//LongClickListener
mChartView.setOnLongClickListener(new View.OnLongClickListener() {
// #Override
public boolean onLongClick(View v) {
SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
Toast.makeText(Clickable2.this, "ON LONG CLICK", Toast.LENGTH_SHORT).show();
if (seriesSelection == null) {
Toast.makeText(Clickable2.this, "No chart element was long pressed",
Toast.LENGTH_SHORT);
return false; // no chart element was long pressed, so let something
// else handle the event
} else {
Toast.makeText(Clickable2.this, "Chart element in series index "
+ seriesSelection.getSeriesIndex() + " data point index "
+ seriesSelection.getPointIndex() + " was long pressed", Toast.LENGTH_SHORT);
return true; // the element was long pressed - the event has been
// handled
}
}
});
mChartView.addZoomListener(new ZoomListener() {
public void zoomApplied(ZoomEvent e) {
String type = "out";
if (e.isZoomIn()) {
type = "in";
}
System.out.println("Zoom " + type + " rate " + e.getZoomRate());
}
public void zoomReset() {
System.out.println("Reset");
}
}, true, true);
mChartView.addPanListener(new PanListener() {
public void panApplied() {
System.out.println("New X range=[" + renderer.getXAxisMin() + ", " + renderer.getXAxisMax()
+ "], Y range=[" + renderer.getYAxisMax() + ", " + renderer.getYAxisMax() + "]");
}
});
layout.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
boolean enabled = mDataset.getSeriesCount() > 0;
} else {
mChartView.repaint();
Toast.makeText(Clickable2.this, "mChartView is NOT null", Toast.LENGTH_SHORT).show();
}
}
Can you check your code for some definition of an object like XYMultipleSeriesRenderer?
Maybe you forgot to set the setClickEnabled method to true?!
Or maybe you should try:
setOnTouchListener
with the method:
public boolean onTouch(View v, MotionEvent event)
It works for me.
Change the value of parameter of setSelectableBuffer method to a bigger number.

Categories

Resources