Skip to content

Commit

Permalink
use osm for rev geo
Browse files Browse the repository at this point in the history
  • Loading branch information
hari01584 committed Aug 25, 2022
1 parent b1bcdb8 commit 9e51aae
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
18 changes: 18 additions & 0 deletions lib/api/map_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:mera_aadhar/fixtures/nearby_center_fixture.dart';
import 'package:mera_aadhar/models/operator_model.dart';
import 'package:mera_aadhar/models/operator_data_model.dart';
import 'package:mera_aadhar/models/nearby_api_model.dart';
import 'package:mera_aadhar/models/osm_reverse_geo.dart' as OSM;

import 'package:mera_aadhar/fixtures/nearby_center_fixture.dart';

import 'package:mera_aadhar/firebase/operator_db.dart';
Expand Down Expand Up @@ -36,6 +39,21 @@ Future<NearbyApiResponse> fetchMapdataFixture(String lat, String lon, int rad) a
return NearbyApiFixture.dummyNearbyApi();
}

Future<String?> getAddressByLatLonNomin(double lat, double lng) async {
int zoom = 14;
String path = "https://nominatim.openstreetmap.org/reverse.php?format=json&lat=$lat&lon=$lng&zoom=$zoom";
final response = await http
.get(Uri.parse(path));

if (response.statusCode == 200) {
var res = OSM.OSMReverse.fromJson(jsonDecode(response.body));
return res.displayName;
} else {
return "Unknown Location";
}

}

Future<String?> getAddressByLatLon(double lat, double lng) async{
GeoCode geoCode = GeoCode();
Address address = await geoCode.reverseGeocoding(latitude: lat, longitude: lng);
Expand Down
92 changes: 92 additions & 0 deletions lib/models/osm_reverse_geo.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
class OSMReverse {
int? placeId;
String? licence;
String? osmType;
int? osmId;
String? lat;
String? lon;
String? displayName;
Address? address;
List<String>? boundingbox;

OSMReverse(
{this.placeId,
this.licence,
this.osmType,
this.osmId,
this.lat,
this.lon,
this.displayName,
this.address,
this.boundingbox});

OSMReverse.fromJson(Map<String, dynamic> json) {
placeId = json['place_id'];
licence = json['licence'];
osmType = json['osm_type'];
osmId = json['osm_id'];
lat = json['lat'];
lon = json['lon'];
displayName = json['display_name'];
address =
json['address'] != null ? new Address.fromJson(json['address']) : null;
boundingbox = json['boundingbox'].cast<String>();
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['place_id'] = this.placeId;
data['licence'] = this.licence;
data['osm_type'] = this.osmType;
data['osm_id'] = this.osmId;
data['lat'] = this.lat;
data['lon'] = this.lon;
data['display_name'] = this.displayName;
if (this.address != null) {
data['address'] = this.address!.toJson();
}
data['boundingbox'] = this.boundingbox;
return data;
}
}

class Address {
String? hamlet;
String? county;
String? iSO31662Lvl6;
String? state;
String? iSO31662Lvl4;
String? country;
String? countryCode;

Address(
{this.hamlet,
this.county,
this.iSO31662Lvl6,
this.state,
this.iSO31662Lvl4,
this.country,
this.countryCode});

Address.fromJson(Map<String, dynamic> json) {
hamlet = json['hamlet'];
county = json['county'];
iSO31662Lvl6 = json['ISO3166-2-lvl6'];
state = json['state'];
iSO31662Lvl4 = json['ISO3166-2-lvl4'];
country = json['country'];
countryCode = json['country_code'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['hamlet'] = this.hamlet;
data['county'] = this.county;
data['ISO3166-2-lvl6'] = this.iSO31662Lvl6;
data['state'] = this.state;
data['ISO3166-2-lvl4'] = this.iSO31662Lvl4;
data['country'] = this.country;
data['country_code'] = this.countryCode;
return data;
}
}
2 changes: 1 addition & 1 deletion lib/screens/operator_selection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class _OperatorSelectionScreenState extends State<OperatorSelectionScreen> {
var match = matches.elementAt(0);
pinLocation =
LatLng(double.parse(match.group(1)!), double.parse(match.group(2)!));
_locationText = (await getAddressByLatLon(
_locationText = (await getAddressByLatLonNomin(
pinLocation.latitude, pinLocation.longitude))!;
} else {
_locationText = place.formattedAddress!;
Expand Down

0 comments on commit 9e51aae

Please sign in to comment.