我创建了一个由Google地图和地方列表视图组成的应用。场所的数据与经度和经度一起在SQLite中。我决定添加一个按钮,如果用户点击它,附近的位置将根据列表视图中用户的位置显示,它将替换先前的列表视图,标记将出现在地图中。单击按钮后,我不知道如何显示附近的位置。在我的项目中,所有的地方列表已经显示在列表视图中,一旦用户在列表中选择,地图将自动获得经度的纬度。请帮帮我。
public class MainActivity extends AppCompatActivity implements LocationListener {
GoogleMap map;
List<LocationModel> GetLocation;
Context context = this;
DatabaseHelper dbhelper;
DatabaseHelper db = new DatabaseHelper(this);
ListView lv;
View yourListView,yourProfileView;
Button buttonnearby;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbhelper = new DatabaseHelper(MainActivity.this);
buttonnearby = (Button) findViewById(R.id.button);
buttonnearby.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View convertView) {
}
});
try{
dbhelper.createDataBase();
}
catch(IOException e){
e.printStackTrace();
}
try {
dbhelper.openDataBase();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
GetLocation = dbhelper.getLocation();
lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(new ViewAdapter());
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
LatLng latlngtofocus = new LatLng(Double.parseDouble(GetLocation.get(i).getlatitude()), Double.parseDouble(GetLocation.get(i).getlongitude()));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latlngtofocus, 17.0f));
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latlngtofocus);
//adding marker to the map
map.addMarker(markerOptions);
yourListView = findViewById(R.id.layout);
ViewGroup parent = (ViewGroup) yourListView.getParent();
parent.removeView(yourListView);
// inflate your profile view (or get the reference to it if it's already inflated)
yourProfileView = getLayoutInflater().inflate(R.layout.profile_location, parent, false);
// add it to the parent
parent.addView(yourProfileView);
}
});
//To get MapFragment reference from xml layout
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
//To get map object
map = mapFragment.getMap();
map.getUiSettings().setZoomControlsEnabled(true);
/* //to show current location in the map
map.setMyLocationEnabled(true);
map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
Toast.makeText(getApplicationContext(), latLng.toString(), Toast.LENGTH_LONG).show();
}
});*/
//To setup location manager
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//To request location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, this);
}
@Override
public void onLocationChanged(Location location) {
//To clear map data
map.clear();
//To hold location
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
//To create marker in map
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("My Location");
//adding marker to the map
map.addMarker(markerOptions);
//opening position with some zoom level in the map
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17.0f));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
/****************************************************************************************
* CUSTOM LIST
****************************************************************************************/
public class ViewAdapter extends BaseAdapter {
LayoutInflater mInflater;
public ViewAdapter() {
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return GetLocation.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_location,null);
}
final TextView place = (TextView) convertView.findViewById(R.id.location);
final TextView latitude = (TextView) convertView.findViewById(R.id.latitude);
final TextView longitude = (TextView) convertView.findViewById(R.id.longitude);
location.setText(GetLocation.get(position).getlocation());
latitude.setText(GetLocation.get(position).getlatitude());
longitude.setText(GetLocation.get(position).getlongitude());
return convertView;
}
}
@Override
public void onBackPressed() {
if(yourProfileView != null && yourProfileView.getParent() != null) {
// remove your profile view
ViewGroup parent = (ViewGroup) yourProfileView.getParent();
parent.removeView(yourProfileView);
// a reference to yourListView has to be saved somewhere; just get it
// add your listview to the parent
parent.addView(yourListView);
} else {
super.onBackPressed();
}
}
}
DatabaseHelper
public List<LocationModel> getLocation() {
List<LocationModel> lList = new ArrayList<LocationModel>();
{
String selectQuery =
"SELECT id,location,latitude,longitude FROM places ";
Log.e("places query: ", selectQuery);
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
LocationModel lm = new LocationModel();
lm.setid(Integer.parseInt(cursor.getString(0)));
lm.setlocation(cursor.getString(1));
lm.setlatitude(cursor.getString(2));
lm.setlongitude(cursor.getString(3));
lList.add(lm);
} while (cursor.moveToNext());
}
db.close();
}
return lList;
}
}
答案 0 :(得分:2)
如果你想获得两个lat lng对之间的距离,最简单的方法是使用<div class="req3">
<h1>Requirement 4</h1>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<br>
Enter info for 4 teams and it will inserted into the database<br><br>
<div class="sqlForm">
<p class="formHead">Team 1</p>
<label>Team Name:</label> <input type="text" name="teamname"><br>
<label>City:</label> <input type="text" name="city"><br>
<label>Best Player:</label> <input type="text" name="bestplayer"><br>
<label>Year Formed:</label> <input type="text" name="yearformed"><br>
<label>Website:</label> <input type="text" name="website"><br>
</div>
<div class="sqlForm">
<p class="formHead">Team 2</p>
<label>Team Name:</label> <input type="text" name="teamname"><br>
<label>City:</label> <input type="text" name="city"><br>
<label>Best Player:</label> <input type="text" name="bestplayer"><br>
<label>Year Formed:</label> <input type="text" name="yearformed"><br>
<label>Website:</label> <input type="text" name="website"><br>
</div>
<div class="sqlForm">
<p class="formHead">Team 3</p>
<label>Team Name:</label> <input type="text" name="teamname"><br>
<label>City:</label> <input type="text" name="city"><br>
<label>Best Player:</label> <input type="text" name="bestplayer"><br>
<label>Year Formed:</label> <input type="text" name="yearformed"><br>
<label>Website:</label> <input type="text" name="website"><br>
</div>
<div class="sqlForm">
<p class="formHead">Team 4</p>
<label>Team Name:</label> <input type="text" name="teamname"><br>
<label>City:</label> <input type="text" name="city"><br>
<label>Best Player:</label> <input type="text" name="bestplayer"><br>
<label>Year Formed:</label> <input type="text" name="yearformed"><br>
<label>Website:</label> <input type="text" name="website"><br><br></div>
<input class="styled-button" type="submit" name="insert" value="Submit">
</form>
<?php
if (isset($_POST['insert'])) {
insertTable();
} else {
$conn->close();
}
function insertTable() {
$servername = "localhost:3306";
$username = "XXXXX";
$password = "XXXXX";
$dbname = "XXXXX";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
echo ("Connection failed: " . $conn->connect_error);
} else {
$varTname = $_POST['teamname'];
$varCity = $_POST['city'];
$varBplayer = $_POST['bestplayer'];
$varYearformed = $_POST['yearformed'];
$varWebsite = $_POST['website'];
$sql = "INSERT INTO Teams (teamname, city, bestplayer, yearformed, website)
VALUES ('$varTname', '$varCity', '$varBplayer', '$varYearformed', '$varWebsite'),
('$varTname', '$varCity', '$varBplayer', '$varYearformed', '$varWebsite'),
('$varTname', '$varCity', '$varBplayer', '$varYearformed', '$varWebsite'),
('$varTname', '$varCity', '$varBplayer', '$varYearformed', '$varWebsite')";
if ($conn->multi_query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
mysql_query($sql);
function PrepSQL($value)
{
// Stripslashes
if(get_magic_quotes_gpc())
{
$value = stripslashes($value);
}
// Quote
$value = "'" . mysql_real_escape_string($value) . "'";
return($value);
}
}
}
?>
中的.distanceBetween()
函数:
Location
答案 1 :(得分:0)
获取用户位置后,您需要计算位置列表之间的距离,并根据您可能希望向用户显示的半径限制在地图上显示该距离,例如:显示2英里内的位置当前用户位置的半径。
答案 2 :(得分:0)
请记住,在某些情况下,长和拉之间的距离可能会有所不同。在北极,两个长点之间的距离与赤道附近的距离不大(请参阅下面的维基百科图片以获得一个想法)。 您可以通过此功能解决此特定问题:
public static double geoCoordToMeter(double latA, double lonA, double latB, double lonB) {
double earthRadius = 6378.137d; // km
double dLat = (latB - latA) * Math.PI / 180d;
double dLon = (lonB - lonA) * Math.PI / 180d;
double a = Math.sin(dLat / 2d) * Math.sin(dLat / 2d)
+ Math.cos(latA * Math.PI / 180d)
* Math.cos(latB * Math.PI / 180d)
* Math.sin(dLon / 2d) * Math.sin(dLon / 2);
double c = 2d * Math.atan2(Math.sqrt(a), Math.sqrt(1d - a));
double d = earthRadius * c;
return (d * 1000d);
}
话虽如此,您将专注于您收到的价值观。在这里,您可以遍历数据库并检查最低的数据。处理这个并不是那么难......伪编码:
private int mMinValue = 10000; // field (member which stores the lowest..)
private checkMin(int newMin){
if(mMinValue < newMin) mMinValue = newMin;
}
如果您不想自己处理地理数据,那么Geofence-API可能会为您做数学处理。 ;)我没有任何关于这个API的实验,也许你必须做一些研究,知道它是否有用。
希望它有所帮助。