Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a popup onTap that stays above marker regardless of zoom #354

Closed
garrrettt opened this issue Jul 9, 2019 · 5 comments
Closed

Comments

@garrrettt
Copy link

In #184 and #75, the solution to creating a popup similar to Google Map's Info Window was to create another marker to be used as a popup that becomes visible when the first marker is tapped.

I implemented this advice, but keeping the popup directly above the marker is difficult, especially when the zoom changes. I tried specifying a LatLng for the popup that had a certain offset from the marker, but when the zoom changes, the popup and marker either get squished together or become too far apart.

Do you have any ideas how I might keep the popup directly above the marker, regardless of zoom?

@johnpryan
Copy link
Collaborator

Can you post a sample that helps clarify the issue?

@garrrettt
Copy link
Author

garrrettt commented Jul 24, 2019

Yes, here's an example. Notice that because I defined the info window's position as a specific LatLng, it gets further away from the marker when you zoom in and close to the marker when you zoom out.

import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong/latlong.dart';

void main() => runApp(MaterialApp(home: PopupExample()));

class PopupExample extends StatefulWidget {
  @override
  _PopupExampleState createState() => _PopupExampleState();
}

class _PopupExampleState extends State<PopupExample> {
  bool infoWindowVisible = false;
  LatLng markerCoords = LatLng(35.754584, -83.974536);
  double infoWindowOffset = 0.002;
  LatLng infoWindowCoords;

  @override
  void initState() {
    super.initState();

    infoWindowCoords = LatLng(
      markerCoords.latitude + infoWindowOffset,
      markerCoords.longitude,
    );
  }

  @override
  Widget build(BuildContext context) {
    return FlutterMap(
      options: MapOptions(
        center: markerCoords,
        zoom: 16.0,
      ),
      layers: [
        TileLayerOptions(
          urlTemplate:
              'https://api.mapbox.com/v4/{id}/{z}/{x}/{y}@2x.png?access_token={accessToken}',
          additionalOptions: {
            'accessToken':
                '[redacted]',
            'id': 'mapbox.streets',
          },
        ),
        MarkerLayerOptions(
          markers: [
            if (infoWindowVisible)
              Marker(
                width: 200.0,
                height: 200.0,
                point: infoWindowCoords,
                builder: (BuildContext ctx) {
                  return Container(color: Colors.orange);
                },
              ),
            Marker(
              point: markerCoords,
              builder: (BuildContext ctx) {
                return GestureDetector(
                  onTap: () {
                    setState(() {
                      infoWindowVisible = true;
                    });
                  },
                  child: Icon(Icons.location_on, size: 50.0,),
                );
              },
            ),
          ],
        )
      ],
    );
  }
}

Here's an example of Google Map's Info Window: https://developers.google.com/maps/documentation/javascript/examples/infowindow-simple

@johnpryan
Copy link
Collaborator

You should be able to draw a widget above the marker in the Marker's builder method. Please feel free to reopen if that doesn't work.

@hardikJoshi123
Copy link

Facing same problem. @garrrettt Did you managed to implement this? Thanks!

@BriceFab
Copy link

BriceFab commented Aug 27, 2024

@garrrettt Remove your accessToken ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants