JSON数据:我想要的是从服务器获取所有纬度,经度,post_title,map_icon - 我该怎么做?我对AsyncTask有所了解,我试试看。我能做什么?我不了解谷歌地图我正在研究它。
{
"action": "all_products",
"success": 1,
"all_products": [
{
"ID": "5689",
"post_title": "Fruitique",
"latitude": "50.85632",
"longitude": "0.58126",
"map_icon": "http:/192.168.1.3/googlemapicons/fruitandveg.png"
},
{
"ID": "5691",
"post_title": "Gourmet Shop Delicatessen",
"latitude": "50.85517",
"longitude": "0.57734",
"map_icon": "http:/192.168.1.3/googlemapicons/deli.png"
},
{
"ID": "5693",
"post_title": "Penbuckles Delicatessen",
"latitude": "50.85692",
"longitude": "0.59099",
"map_icon": "http:/192.168.1.3/googlemapicons/10percent.png"
},
{
"ID": "5699",
"post_title": "Rainbows Sweetshop",
"latitude": "50.856011",
"longitude": "0.581003",
"map_icon": "http:/192.168.1.3/googlemapicons/10percent.png"
},
{
"ID": "5701",
"post_title": "Sultans Supermarket",
"latitude": "50.85517",
"longitude": "0.57734",
"map_icon": "http:/192.168.1.3/googlemapicons/save£5.png"
},
{
"ID": "5705",
"post_title": "Arcade Butchers",
"latitude": "50.85632",
"longitude": "0.58126",
"map_icon": "http:/192.168.1.3/googlemapicons/save50p.png"
}
]
}
我尝试了什么 -
public class MainActivityMap extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap;
ArrayList<All_products_lat_long> venueList;
protected int getLayoutId() {
return R.layout.frnt_map_activity_map;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
setUpMap();
}
@Override
protected void onResume() {
super.onResume();
setUpMap();
}
@Override
public void onMapReady(GoogleMap map) {
if (mMap != null) {
new MarkerTask().execute();
}
mMap = map;
}
private void setUpMap() {
((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
}
/**
* Run the demo-specific code.
*/
protected GoogleMap getMap() {
return mMap;
}
private class MarkerTask extends AsyncTask<Void, Void, String> {
private static final String LOG_TAG = "ExampleApp";
private static final String SERVICE_URL = "https://www.towncitycards.com/webservice_action.php?action=all_products";
// Invoked by execute() method of this object
@Override
protected String doInBackground(Void... args) {
HttpURLConnection conn = null;
final StringBuilder json = new StringBuilder();
try {
// Connect to the web service
URL url = new URL(SERVICE_URL);
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
// Read the JSON data into the StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
json.append(buff, 0, read);
}
} catch (IOException e) {
Log.e(LOG_TAG, "Error connecting to service", e);
//throw new IOException("Error connecting to service", e); //uncaught
} finally {
if (conn != null) {
conn.disconnect();
}
}
return json.toString();
}
// Executed after the complete execution of doInBackground() method
@Override
protected void onPostExecute(String json) {
for (int k = 0; k < venueList.size(); k += 1) {
try {
double ln = Double.parseDouble(venueList.get(k).getLongitude());
double la = Double.parseDouble(venueList.get(k).getLatitude());
// De-serialize the JSON string into an array of city objects
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
LatLng latLng = new LatLng( Double.parseDouble(jsonObj.getString("latitude")),
Double.parseDouble( jsonObj.getString("longitude")));
//move CameraPosition on first result
if (i == 0) {
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLng).zoom(13).build();
mMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
// Create a marker for each city in the JSON data.
mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
.title(jsonObj.getString("post_title"))
.position(latLng));
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Error processing JSON", e);
}
}
}
}
}
答案 0 :(得分:0)
这可以帮助
mapView = (MapView) rootView.findViewById(R.id.aboutUs_map_Maps);
mapView.onCreate(savedInstanceState);
mapView.onResume();
MapsInitializer.initialize(getActivity().getApplicationContext());
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap = googleMap;
if (ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
googleMap.setMyLocationEnabled(true);
LatLng pune=new LatLng(18.5,73.8);
googleMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)).anchor(0.0f,1.0f).position(pune).title("Pune City").snippet("Find the colleges around pune city"));
CameraPosition cameraPosition=new CameraPosition.Builder().target(pune).zoom(12).build();
}
});
您可以根据您的json数据更改LatLng pune=new LatLng(18.5,73.8);
pune位置的位置