liodali / osm_flutter

OpenStreetMap plugin for flutter
https://pub.dev/packages/flutter_osm_plugin
MIT License
229 stars 95 forks source link

Persisting currentLocation #8

Closed batyanko closed 4 years ago

batyanko commented 4 years ago

Hi, I am trying to show a GeoPoint as initPosition, but the OSMFlutter widget persists showing user's location when loading. (Actually it seems to show the GeoPoint position for a few ms, then going directly to user's location) A related problem is the app asking for location permission, which I prefer to keep off for now.

currentLocation and trackMyPosition are both set to false.

I am showing the widget in the following way, invoked as a BottomNavigationBarItem:

Widget getContacts() {
    GlobalKey<OSMFlutterState> osmKey = GlobalKey<OSMFlutterState>();

    var col = new Column(
      children: <Widget>[
        Expanded(
            child: OSMFlutter(
          key: osmKey,
          currentLocation: false,
          road: Road(
            startIcon: MarkerIcon(
              icon: Icon(
                Icons.person,
                size: 64,
                color: Colors.brown,
              ),
            ),
            roadColor: Colors.red,
          ),
          markerIcon: MarkerIcon(
            icon: Icon(
              Icons.assistant_photo,
              color: Colors.blue,
              size: 80,
            ),
          ),
          initPosition: GeoPoint(latitude: 42.151753, longitude: 24.769730),
          showZoomController: true,
          trackMyPosition: false,
        )),
        IconButton(
          icon: Icon(Icons.ac_unit),
          onPressed: () {
//            goToLocation();
            MapsLauncher.launchCoordinates(42.151753, 24.769730);
          },
        )
      ],
    );

//    osmKey.currentState.changeLocation(
//        GeoPoint(latitude: 42.151753, longitude: 24.769730));

    return col;
  }
...
environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  maps_launcher: ^1.2.0
  flutter_osm_plugin: ^0.3.6+3
...

The project is quite small, you can try it yourself: https://github.com/batyanko/table-tennis-club/commit/a5b227b773d8985408e54065d5c2f0cb4e5e2dc2

Tested on Galaxy S7 and the API 30 Emulator (w/GooglePlay)

Note: I am new to Flutter, might have done various things wrong. Let me know.

liodali commented 4 years ago

hi, why you want to show map as item in navigationBar ?

batyanko commented 4 years ago

Yeah it is supposed to be a simple app of a business. You know, first tab is some info, second tab for example photos, and third tab is "Contacts".

Something like your widget would fit very nicely in "Contacts", but it needs to init on the address of the business (and not the user's location).

Now I tried to put the Widget in a very simple second screen class. Calling it with Navigator.push, it opens but result is the same - sending me to the users location instead of the init location.

Where should I put the Widget so that it works? So I can start from there...

liodali commented 4 years ago

look to this example :

import 'package:flutter/material.dart';
import 'package:flutter_osm_plugin/flutter_osm_plugin.dart';

class SimpleExample extends StatefulWidget {
  SimpleExample({Key key}) : super(key: key);

  @override
  _SimpleExampleState createState() => _SimpleExampleState();
}

class _SimpleExampleState extends State<SimpleExample> {
  PageController controller;
  int indexPage;

  @override
  void initState() {
    super.initState();
    controller = PageController(initialPage: 1);
    indexPage = controller.initialPage;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("osm"),
      ),
      body: PageView(
        children: <Widget>[
          Center(
            child: Text("page n1"),
          ),
          SimpleOSM(),
        ],
        controller: controller,
        onPageChanged: (p) {
          setState(() {
            indexPage = p;
          });
        },
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: indexPage,
        onTap: (p) {
          controller.animateToPage(p,
              duration: Duration(milliseconds: 500), curve: Curves.linear);
        },
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.info),
            title: Text("information"),
          ),
          BottomNavigationBarItem(
              icon: Icon(Icons.contacts), title: Text("contact")),
        ],
      ),
    );
  }
}

class SimpleOSM extends StatefulWidget {
  @override
  State<StatefulWidget> createState() =>SimpleOSMState();
}

class SimpleOSMState extends State<SimpleOSM> with AutomaticKeepAliveClientMixin {
  GlobalKey<OSMFlutterState> osmKey;

  @override
  void initState() {
    super.initState();
    osmKey = GlobalKey<OSMFlutterState>();
  }

  @override
  Widget build(BuildContext context) {
    return OSMFlutter(
      key: osmKey,
      currentLocation: false,
      markerIcon: MarkerIcon(
        icon: Icon(
          Icons.person_pin_circle,
          color: Colors.blue,
          size: 56,
        ),
      ),
      trackMyPosition: false,
      initPosition: GeoPoint(latitude: 42.151753, longitude: 24.769730),
      useSecureURL: false,
    );
  }

  @override
  bool get wantKeepAlive => true;
}
batyanko commented 4 years ago

Thanks for the effort (and for the page controller, that's useful) I put this in a new file and invoke it with Navigator.push. It works, but the same problem is there - it brings me to Silicon Valley again :)) (default device location of the Emulator). Should I invoke this with anything other than Navigator.push?

liodali commented 4 years ago

i tested in my phone and it's worked fine , i will do more test to find problem try to run the app with real device and tell me if the problem still exist.