I am not able to make marker a button to open a new activity, I am getting information of the coordinates from GeoJson
file but and able to get the marker but the marker is not going to new activity.
I have tried setOnClickListener
and setOnWindowClickListner
as used in some of the queries but to no avail
@Override
public void onMapReady(GoogleMap googleMap) {
// entering the google maps setting
googleMap.getUiSettings().setZoomControlsEnabled(true);
ArrayList<LatLng> listPoints = new ArrayList<LatLng>();
googleMap.setTrafficEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(Sg));
googleMap.setMinZoomPreference(11);
boolean success = googleMap.setMapStyle(new MapStyleOptions(getResources()
.getString(R.string.style_json)));
if (!success) {
Log.e(TAG, "Style parsing failed.");
}
try {`//getting data from geojson`
getData();
JSONObject emptyGeoJson = new JSONObject();
layerShop = new GeoJsonLayer(googleMap, emptyGeoJson);
layerFB = new GeoJsonLayer(googleMap, emptyGeoJson);
layerComm = new GeoJsonLayer(googleMap, emptyGeoJson);
GeoJsonLayer layer = new GeoJsonLayer(googleMap, HotspotData);
Iterable<GeoJsonFeature> geoJsonFeature = layer.getFeatures();
for(GeoJsonFeature cur : geoJsonFeature) {
String s = cur.getProperty("Description");
int begin = s.indexOf("<th>LOCATION_TYPE</th> <td>");
int end = s.indexOf("</td>",begin);
String category = s.substring(begin+27,end);
if (category.contentEquals("F&B")){
layerFB.addFeature(cur);
Log.v("MapsActivity", "So here we are with " + cur);
}
else if (category.contentEquals("Shopping Mall")){
layerShop.addFeature(cur);
}
else if (category.contentEquals("Community")){
layerComm.addFeature(cur);
}
}
layers.put("Community", layerComm);
layers.put("Cafes", layerFB);
layers.put("Shopping", layerShop);
} catch (Exception e) {
e.printStackTrace();
}
GeoJsonPointStyle pointStyle = layerFB.getDefaultPointStyle();
pointStyle.setIcon(bitmapDescriptorFromVector(this, R.drawable.ic_wifi));
pointStyle.setTitle("View information");
//Log.v("MapsActivity", "This is layerFB + " + layerFB.getFeatures());
Iterable<GeoJsonFeature> layerFBfeature = layerFB.getFeatures();
Iterable<GeoJsonFeature> layerShopfeature = layerShop.getFeatures();
Iterable<GeoJsonFeature> layerCommfeature = layerComm.getFeatures();
Marker marker;
for(GeoJsonFeature i: layerFBfeature){
LatLng is = (LatLng)i.getGeometry().getGeometryObject();
marker = googleMap.addMarker(new MarkerOptions().position(is).title("location"));
}
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Intent intent = new Intent(MapsActivity.this, activity_location.class);
startActivity(intent);
return true;
}
});
}
Your code works fine for me. Is activity_location
an existing, valid activity and did you add it to your Android Manifest?
Try the below code as a working example.
MapsActivity.java
import androidx.fragment.app.FragmentActivity;
import android.content.Intent;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
if (mapFragment != null) {
mapFragment.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Intent intent = new Intent(MapsActivity.this, HelloWorldActivity.class);
startActivity(intent);
return true;
}
});
}
}
HelloWorldActivity.java
import android.os.Bundle;
import android.widget.Toast;
import androidx.fragment.app.FragmentActivity;
public class HelloWorldActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(this, "in HelloWorldActivity", Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:allowBackup="true">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HelloWorldActivity" />
</application>
</manifest>
Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.