I am working on app
and there are button, when i clicked on, it will display latitude and longitude for (current location and marker that selected)
for selected marker i create Custom Info Window Adapter
in this class there are latitude and longitude for selected marker so in CustomInfoWindowAdapter.java
i have created GET
method for (latitude and longitude )
and in MainActivityMaps.java
i created method for button and inside this method i created (two methods for display current and marker selected)
current work well but i have problem (Error) at (marker selected) app crash
MainActivity.java
// button onclick call to show latitude and longitude as a toast for test
public void showcurrentlocationattost(View view){
displaytestcurrent(mLastLocation);
displayplace(mCurrLocationMarker); // mCurrLocationMarker -> just name
}
public void displaytestcurrent(Location location){
LatLng latLngcurrent = new LatLng(location.getLatitude(), location.getLongitude());
String text = (String.valueOf("latitude = "+latLngcurrent.latitude+" , "+"longitude = "+ latLngcurrent.longitude));
Toast.makeText(getApplicationContext(), text,
Toast.LENGTH_SHORT).show();
}
public void displayplace(Marker marker){
Context context = getApplicationContext();
CustomInfoWindowAdapter op = new CustomInfoWindowAdapter(context); // access getlati for latitude and getlongi //for longitude From this class //CutomInfoWindowAdapter.java
String text = (String.valueOf("latitude = "+op.getlati()+" , "+"longitude = "+ op.getlongi()));
Toast.makeText(getApplicationContext(), text,Toast.LENGTH_SHORT).show();
// for test
Log.d("testL", String.valueOf(op.getlati()));
}
CutomInfoWindowAdapter.java
package com.example.mostafa.parking;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter{
private Marker marker;
private final View mWindow;
private Context mContext;
double lati=0.0;
double longi=0.0 ;
String locat;
public CustomInfoWindowAdapter(Context context){
mContext = context;
mWindow = LayoutInflater.from(context).inflate(R.layout.custom_info_window,null);
}
private double rendomWindowtext(Marker marker, View view){
LatLng location = marker.getPosition();
String title = marker.getTitle();
TextView tvTitle = (TextView) view.findViewById(R.id.title);
TextView tvLocat = (TextView) view.findViewById(R.id.locat);
lati = location.latitude;
longi = location.longitude;
if (!title.equals("")){
tvTitle.setText(title);
}
if (!location.equals("")){
tvLocat.setText(String.valueOf(lati));
}
Log.d("lati", String.valueOf(lati));
Log.d("longi", String.valueOf(longi));
locat= String.valueOf(location);
Log.d("Location", String.valueOf(location.latitude));
String text = (String.valueOf("latitude = "+lati+" , "+"longitude = "+ longi));
Toast.makeText(mContext.getApplicationContext(), text,
Toast.LENGTH_SHORT).show();
return lati;
}
public double getlati(){
LatLng location = marker.getPosition();
return location.latitude;
}
public double getlongi(){
LatLng location = marker.getPosition();
return location.longitude;
}
@Override
public View getInfoWindow(Marker marker) {
rendomWindowtext(marker,mWindow);
return mWindow;
}
@Override
public View getInfoContents(Marker marker) {
rendomWindowtext(marker,mWindow);
return mWindow;
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivityMaps" />
<--onClick="showcurrentlocationattost -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Direction"
android:onClick="showcurrentlocationattost"/>
</RelativeLayout>
Errors
Process: com.example.mostafa.parking, PID: 5750
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4700)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22288)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4695)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22288)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.LatLng com.google.android.gms.maps.model.Marker.getPosition()' on a null object reference
at com.example.mostafa.parking.CustomInfoWindowAdapter.getlati(CustomInfoWindowAdapter.java:62)
at com.example.mostafa.parking.MainActivityMaps.displayplace(MainActivityMaps.java:334)
at com.example.mostafa.parking.MainActivityMaps.showcurrentlocationattost(MainActivityMaps.java:306)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4695)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22288)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)